VBA MACRO被困在一个循环中

时间:2014-05-22 14:01:04

标签: vba

我在VBA的Thinking for IDX中工作。我可以让它进入循环,但它永远不会离开循环。

While returnValue = osCurrentScreen.WaitForString3(LF & "Reload Run ", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
     If (returnValue <> ReturnCode_Success) Then
         While returnValue = osCurrentScreen.WaitForString3(LF & "Load new Run for date ", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
         Wend
     Else
         osCurrentScreen.SendKeys "N"
         osCurrentScreen.SendControlKey ControlKeyCode_Return
     End If
Wend

它将进入循环但被卡住了。

我尝试使用do loopdo whilewhile wend。我已经更改了提示的措辞,有时没有进入循环,卡在循环中,或者完全跳过了循环。

第二次尝试使用do循环 - 相同的结果它将进入循环但不会离开并继续下一步

        Do

            If returnValue = osCurrentScreen.WaitForString3(LF & "Reload Run ", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes) Then
           osCurrentScreen.SendKeys "N"
           osCurrentScreen.SendControlKey ControlKeyCode_Return

          Else
     'how do i add end loop here? end exit and wend do not work  
     End If

   Loop

我认为问题在于它实际上并没有阅读提示,只是跳过了做任何想做的事情。

在循环中尝试#3

  Do While returnValue = osCurrentScreen.WaitForString3(LF & "Reload Run ", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)

 'returnValue = osCurrentScreen.WaitForString3(LF & "Reload Run ", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
 If returnValue = ReturnCode_Success Then
    osCurrentScreen.SendKeys "N"
    osCurrentScreen.SendControlKey ControlKeyCode_Return

'returnValue = osCurrentScreen.WaitForString3("? N=> ", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)

  '  Exit Do

   ' returnValue = osCurrentScreen.WaitForString3(LF & "Load new Run for date ", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
 Else
     osCurrentScreen.SendKeys "Y"
     osCurrentScreen.SendControlKey ControlKeyCode_Return


    End If
Exit Do
 Loop

以下完整代码

Sub Main()
        Const NEVER_TIME_OUT = 0
        Const DIALOG_TITLE = "idxlog"


        Dim filedata As String
        Dim row As Integer
        Dim singleFile As String
        Dim strdate As String
        strdate = Format(Date, "mmddyyyy")
        Dim osCurrentScreen As Screen
        Dim osCurrentTerminal As Terminal
        Dim returnValue As Integer

        Set osCurrentTerminal = ThisFrame.SelectedView.control
        Set osCurrentScreen = osCurrentTerminal.Screen

        NUL = Chr(0)
        LF = Chr(10)
        CR = Chr(13)
        ESC = Chr(27)

        singleFile = Dir$("C:\CHG" + strdate + ".DAT")
        If singleFile <> "" Then
        FileCopy "C:\CHG" + strdate + ".DAT", "C:\ CHG" + strdate + ".SAVE"
         End If






        If canceled Then Exit Sub


    With Application
        'log into TEST


'SELECT BAR
    'FOR TEST UNCOMMENT
    osCurrentScreen.SendKeys "1"
    'FOR LIVE UNCOMMENT
    'osCurrentScreen.SendKeys "3"
    osCurrentScreen.SendControlKey ControlKeyCode_Return
    'Wait for a string on the host screen before continuing
    returnValue = osCurrentScreen.WaitForString3(LF & "Enter Group: ", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
    If (returnValue <> ReturnCode_Success) Then
        Err.Raise 11001, "WaitForString3", "Timeout waiting for string.", "VBAHelp.chm", "11001"
    End If
'SELECT GROUP
    osCurrentScreen.SendKeys "3"
    osCurrentScreen.SendControlKey ControlKeyCode_Return
    'Wait for a string on the host screen before continuing
    returnValue = osCurrentScreen.WaitForString3(LF & "Select Function: ", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
    If (returnValue <> ReturnCode_Success) Then
        Err.Raise 11001, "WaitForString3", "Timeout waiting for string.", "VBAHelp.chm", "11001"
    End If
'Step 5
'SELECT SIU INTERFACES
    osCurrentScreen.SendKeys "36"
    osCurrentScreen.SendControlKey ControlKeyCode_Return
    'Wait for a string on the host screen before continuing
    returnValue = osCurrentScreen.WaitForString3(LF & "Select Activity: ", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
    If (returnValue <> ReturnCode_Success) Then
        Err.Raise 11001, "WaitForString3", "Timeout waiting for string.", "VBAHelp.chm", "11001"
    End If
'Step 6
'SELECT CHARGE TO TES INTERFACE ACTIVITIES
    osCurrentScreen.SendKeys "50"
    osCurrentScreen.SendControlKey ControlKeyCode_Return
    'Wait for a string on the host screen before continuing
    returnValue = osCurrentScreen.WaitForString3(BS & ESC & "[25;1H", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
    If (returnValue <> ReturnCode_Success) Then
        Err.Raise 11001, "WaitForString3", "Timeout waiting for string.", "VBAHelp.chm", "11001"
    End If
'Step 7
'TYPE L TO LOAD INTERFACE
    osCurrentScreen.SendKeys "L"
    'Wait for a string on the host screen before continuing
    returnValue = osCurrentScreen.WaitForString3(LF & "Charge Interface Run Date: ", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
    If (returnValue <> ReturnCode_Success) Then
        Err.Raise 11001, "WaitForString3", "Timeout waiting for string.", "VBAHelp.chm", "11001"
    End If
'Step 8
'DATE T=TODAY
    osCurrentScreen.SendKeys "T"
    osCurrentScreen.SendControlKey ControlKeyCode_Return



 'While returnValue = osCurrentScreen.WaitForString3(LF & "Reload Run ", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
 'While returnValue = Ture

 While returnValue = osCurrentScreen.WaitForString3(LF & "Reload Run ", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
 ' (returnValue = osCurrentScreen.WaitForString3("? N=> ", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes))
   '         returnValue = osCurrentScreen.WaitForString3(LF & "Load new Run for date ", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
            If (returnValue <> ReturnCode_Success) Then
               ' Exit Do
               While returnValue = osCurrentScreen.WaitForString3(LF & "Load new Run for date ", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
               Wend
         Else
           osCurrentScreen.SendKeys "N"
           osCurrentScreen.SendControlKey ControlKeyCode_Return
     End If
   ' Loop

 Wend

' Wait (1)




'STEP 9
'LOAD NEW RUN
    'Wait for a string on the host screen before continuing
    returnValue = osCurrentScreen.WaitForString3("? N=> ", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
    If (returnValue <> ReturnCode_Success) Then
    Err.Raise 11001, "WaitForString3", "Timeout waiting for string.", "VBAHelp.chm", "11001"
   End If
    osCurrentScreen.SendKeys "Y"
    osCurrentScreen.SendControlKey ControlKeyCode_Return



'Step 10
'SERIAL NUMBER
    'Wait for a string on the host screen before continuing
    returnValue = osCurrentScreen.WaitForString3(LF & "Serial Number: ", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
   If (returnValue <> ReturnCode_Success) Then
        Err.Raise 11001, "WaitForString3", "Timeout waiting for string.", "VBAHelp.chm", "11001"
    End If
    osCurrentScreen.SendKeys strdate
    osCurrentScreen.SendControlKey ControlKeyCode_Return
    'Wait for a string on the host screen before continuing
    returnValue = osCurrentScreen.WaitForString3(LF & "Description: ", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
    If (returnValue <> ReturnCode_Success) Then
        Err.Raise 11001, "WaitForString3", "Timeout waiting for string.", "VBAHelp.chm", "11001"
    End If

'Description and Device Prompts
    osCurrentScreen.SendKeys "CONTEXT"
    osCurrentScreen.SendControlKey ControlKeyCode_Return
    'Wait for a string on the host screen before continuing
    returnValue = osCurrentScreen.WaitForString3(LF & "Device: ", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
    If (returnValue <> ReturnCode_Success) Then
        Err.Raise 11001, "WaitForString3", "Timeout waiting for string.", "VBAHelp.chm", "11001"
    End If
    osCurrentScreen.SendControlKey ControlKeyCode_Return
    'Wait for a string on the host screen before continuing
    returnValue = osCurrentScreen.WaitForString3("   Right margin: ", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
    If (returnValue <> ReturnCode_Success) Then
        Err.Raise 11001, "WaitForString3", "Timeout waiting for string.", "VBAHelp.chm", "11001"
    End If

    'Wait for a string on the host screen before continuing
    returnValue = osCurrentScreen.WaitForString3("=> ", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
    If (returnValue <> ReturnCode_Success) Then
        Err.Raise 11001, "WaitForString3", "Timeout waiting for string.", "VBAHelp.chm", "11001"
    End If
    osCurrentScreen.SendControlKey ControlKeyCode_Return
    'Wait for a string on the host screen before continuing



'Step 12 -15
'Set source file
    returnValue = osCurrentScreen.WaitForString3(LF & "Enter RMS file source (CCA/CONTEXT): ", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
    If (returnValue <> ReturnCode_Success) Then
        Err.Raise 11001, "WaitForString3", "Timeout waiting for string.", "VBAHelp.chm", "11001"
    End If
    osCurrentScreen.SendKeys "CONTEXT"
    osCurrentScreen.SendControlKey ControlKeyCode_Return
    'Wait for a string on the host screen before continuing
'Step 16
'Process runs
'Step 17
'Reflections will wait on Prompt "Do you Wish To Continue?"  (Y)es
    returnValue = osCurrentScreen.WaitForString3(LF & "Do you wish to continue ? Y=> ", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
    If (returnValue <> ReturnCode_Success) Then
        Err.Raise 11001, "WaitForString3", "Timeout waiting for string.", "VBAHelp.chm", "11001"
    End If
    osCurrentScreen.SendControlKey ControlKeyCode_Return
    'Wait for a string on the host screen before continuing
    returnValue = osCurrentScreen.WaitForString3(LF & "Press <ENTER> to Continue: ", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
    If (returnValue <> ReturnCode_Success) Then
        Err.Raise 11001, "WaitForString3", "Timeout waiting for string.", "VBAHelp.chm", "11001"
    End If
    osCurrentScreen.SendControlKey ControlKeyCode_Return
    'Wait for a string on the host screen before continuing
    returnValue = osCurrentScreen.WaitForString3("r" & ESC & "[16;1H" & ESC & "[1C" & ESC & "[1;7m  ", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
    If (returnValue <> ReturnCode_Success) Then
        Err.Raise 11001, "WaitForString3", "Timeout waiting for string.", "VBAHelp.chm", "11001"
    End If

    'Wait for a string on the host screen before continuing
    returnValue = osCurrentScreen.WaitForString3("  Selected    F", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
    If (returnValue <> ReturnCode_Success) Then
        Err.Raise 11001, "WaitForString3", "Timeout waiting for string.", "VBAHelp.chm", "11001"
    End If

    'Wait for a string on the host screen before continuing
    returnValue = osCurrentScreen.WaitForString3("Q-Quit    F", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
    If (returnValue <> ReturnCode_Success) Then
        Err.Raise 11001, "WaitForString3", "Timeout waiting for string.", "VBAHelp.chm", "11001"
    End If

    'Wait for a string on the host screen before continuing
    returnValue = osCurrentScreen.WaitForString3("-OK    <HELP>-Help    F", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
    If (returnValue <> ReturnCode_Success) Then
        Err.Raise 11001, "WaitForString3", "Timeout waiting for string.", "VBAHelp.chm", "11001"
    End If

    'Wait for a string on the host screen before continuing
    returnValue = osCurrentScreen.WaitForString3("-More Keys           " & ESC & "[0m" & ESC & "[78D" & ESC & "[25;1H", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
    If (returnValue <> ReturnCode_Success) Then
        Err.Raise 11001, "WaitForString3", "Timeout waiting for string.", "VBAHelp.chm", "11001"
    End If
'Step 20 Process Context file
'send "p" to process
    osCurrentScreen.SendKeys "P"
    'Wait for a string on the host screen before continuing
    returnValue = osCurrentScreen.WaitForString3(LF & "Charge Interface Date: ", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
    If (returnValue <> ReturnCode_Success) Then
        Err.Raise 11001, "WaitForString3", "Timeout waiting for string.", "VBAHelp.chm", "11001"
    End If
'step 21
'send date
    osCurrentScreen.SendKeys strdate
    osCurrentScreen.SendControlKey ControlKeyCode_Return
    'Wait for a string on the host screen before continuing
    returnValue = osCurrentScreen.WaitForString3(LF & "Process run ", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
    If (returnValue <> ReturnCode_Success) Then
        Err.Raise 11001, "WaitForString3", "Timeout waiting for string.", "VBAHelp.chm", "11001"
    End If

    'Wait for a string on the host screen before continuing
    returnValue = osCurrentScreen.WaitForString3(" ? N=> ", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
    If (returnValue <> ReturnCode_Success) Then
        Err.Raise 11001, "WaitForString3", "Timeout waiting for string.", "VBAHelp.chm", "11001"
    End If
    osCurrentScreen.SendControlKey ControlKeyCode_Return
    'Wait for a string on the host screen before continuing
    returnValue = osCurrentScreen.WaitForString3(LF & "Charge Interface Date: ", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
    If (returnValue <> ReturnCode_Success) Then
        Err.Raise 11001, "WaitForString3", "Timeout waiting for string.", "VBAHelp.chm", "11001"
    End If
    osCurrentScreen.SendKeys "T"
    osCurrentScreen.SendControlKey ControlKeyCode_Return
    'Wait for a string on the host screen before continuing
    returnValue = osCurrentScreen.WaitForString3(LF & "Process run ", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
    If (returnValue <> ReturnCode_Success) Then
        Err.Raise 11001, "WaitForString3", "Timeout waiting for string.", "VBAHelp.chm", "11001"
    End If

    'Wait for a string on the host screen before continuing
    returnValue = osCurrentScreen.WaitForString3(" ? N=> ", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
    If (returnValue <> ReturnCode_Success) Then
        Err.Raise 11001, "WaitForString3", "Timeout waiting for string.", "VBAHelp.chm", "11001"
    End If
    osCurrentScreen.SendKeys "Y"
    osCurrentScreen.SendControlKey ControlKeyCode_Return
    'Wait for a string on the host screen before continuing
'Step 24
'Do you want to Queue this activity? (N)o
    returnValue = osCurrentScreen.WaitForString3(LF & "Do you want to queue this activity? No=> ", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
    If (returnValue <> ReturnCode_Success) Then
        Err.Raise 11001, "WaitForString3", "Timeout waiting for string.", "VBAHelp.chm", "11001"
    End If

    'might not need
    'osCurrentScreen.SendKeys "N"
    osCurrentScreen.SendControlKey ControlKeyCode_Return
    'Wait for a string on the host screen before continuing
    returnValue = osCurrentScreen.WaitForString3(LF & "Device: ", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
    If (returnValue <> ReturnCode_Success) Then
        Err.Raise 11001, "WaitForString3", "Timeout waiting for string.", "VBAHelp.chm", "11001"
    End If
'Step 25
'Set Dir to DList2 and File Name
    osCurrentScreen.SendKeys "IDXSTAFF:CONTEXT" + strdate + ".TXT"
    osCurrentScreen.SendControlKey ControlKeyCode_Return
    'Wait for a string on the host screen before continuing
    returnValue = osCurrentScreen.WaitForString3("   Parameters: ""RWSN"" => ", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
    If (returnValue <> ReturnCode_Success) Then
        Err.Raise 11001, "WaitForString3", "Timeout waiting for string.", "VBAHelp.chm", "11001"
    End If
'Process is complete from Step 25
    osCurrentScreen.SendControlKey ControlKeyCode_Return

    'Wait for a string on the host screen before continuing
    returnValue = osCurrentScreen.WaitForString3("r" & ESC & "[16;1H" & ESC & "[1C" & ESC & "[1;7m  ", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
    If (returnValue <> ReturnCode_Success) Then
        Err.Raise 11001, "WaitForString3", "Timeout waiting for string.", "VBAHelp.chm", "11001"
    End If

    'Wait for a string on the host screen before continuing
    returnValue = osCurrentScreen.WaitForString3("  Selected    F", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
    If (returnValue <> ReturnCode_Success) Then
        Err.Raise 11001, "WaitForString3", "Timeout waiting for string.", "VBAHelp.chm", "11001"
    End If

    'Wait for a string on the host screen before continuing
    returnValue = osCurrentScreen.WaitForString3("Q-Quit    F", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
    If (returnValue <> ReturnCode_Success) Then
        Err.Raise 11001, "WaitForString3", "Timeout waiting for string.", "VBAHelp.chm", "11001"
    End If

    'Wait for a string on the host screen before continuing
    returnValue = osCurrentScreen.WaitForString3("-OK    <HELP>-Help    F", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
    If (returnValue <> ReturnCode_Success) Then
        Err.Raise 11001, "WaitForString3", "Timeout waiting for string.", "VBAHelp.chm", "11001"
    End If

    'Wait for a string on the host screen before continuing
    returnValue = osCurrentScreen.WaitForString3("-More Keys           " & ESC & "[0m" & ESC & "[78D" & ESC & "[25;1H", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
    If (returnValue <> ReturnCode_Success) Then
        Err.Raise 11001, "WaitForString3", "Timeout waiting for string.", "VBAHelp.chm", "11001"
    End If

'EXIT DONE
' Press VtF7 (Perform the Vt F7 function).
    osCurrentScreen.SendControlKey ControlKeyCode_F7
    osCurrentScreen.SendKeys "Q"
    'Wait for a string on the host screen before continuing
    returnValue = osCurrentScreen.WaitForString3(LF & "Select Activity: ", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
    If (returnValue <> ReturnCode_Success) Then
        Err.Raise 11001, "WaitForString3", "Timeout waiting for string.", "VBAHelp.chm", "11001"
    End If

    osCurrentScreen.SendControlKey ControlKeyCode_Return
    'Wait for a string on the host screen before continuing
    returnValue = osCurrentScreen.WaitForString3(LF & "Select Function: ", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
    If (returnValue <> ReturnCode_Success) Then
        Err.Raise 11001, "WaitForString3", "Timeout waiting for string.", "VBAHelp.chm", "11001"
    End If

    osCurrentScreen.SendControlKey ControlKeyCode_Return

    Exit Sub


  End With
     End Sub 

1 个答案:

答案 0 :(得分:0)

这将是一个无限循环,无论如何(或者它永远不会执行,因为returnValue以与WaitForString的结果相同的值开始,但是我们看不到任何前面的代码让我无法猜测)

While returnValue = osCurrentScreen.WaitForString3(LF & "Load new Run for date ", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
Wend

无法更改returnValue,因为循环体是空的。

至于外循环returnValue永远不会被设置,所以它永远不会改变。

我希望您认为returnValue = osCurrentScreen.WaitForString3(LF & "Load new Run for date ", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)会设置returnValue的值,但事实并非如此。 Wend运算符期望一个布尔值,因此该行测试这两个是否相等并返回true或false。你需要在循环体中设置returnValue。像这样的错误就是像C#这样的语言使用==进行相等比较而使用=进行赋值。 VBA并不是那么幸运。

更新

'Check the initial value
returnValue = osCurrentScreen.WaitForString3(LF & "Reload Run ", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
While returnValue = "your string"
     If (returnValue <> ReturnCode_Success) Then
         'check if you're in the state you want to trigger the exit condition
         While returnValue = "Load new run"
             'get the new current state
             returnValue = osCurrentScreen.WaitForString3(LF & "Load new Run for date ", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
         Wend
     Else
         osCurrentScreen.SendKeys "N"
         osCurrentScreen.SendControlKey ControlKeyCode_Return
     End If
     'Check before looping again
     returnValue = osCurrentScreen.WaitForString3(LF & "Reload Run ", NEVER_TIME_OUT, WaitForOption.WaitForOption_AllowKeystrokes)
Wend