Search code examples
for-loopvbscriptiteratorqtp

For loop in VBScript (QTP) not functioning


I'm using QTP 11.0 which uses VBScript for it's "language".

I have the following four lines of code:

For x = 1 to 8
msgbox(x)
update1 =  SwfWindow("NextGen File Maintenance").SwfWindow("SIM Library Configuration").SwfObject("spreadCtl").Object.ActiveSheet.GetValue(x,2)
Next

The return values are:

1 3 3 3 3 3 ... forever

I can not seem to use the counter variable in a vbscript for loop without it somehow becoming corrupted - no matter what the name of the variable. I have even tried assigning X to another variable and using that in my GetValue statement with no success.

Am I missing something really simple here? No documentation on Google for a vbscript For loop implies any different usage. I found nothing in reference to QTP either.

Thanks,

Jason


Solution

  • This is very strange, I assume that if you remove the second line in the loop and just leave the MsgBox then x is incremented correctly.

    Perhaps ActiveSheet.GetValue takes the first parameter by reference and modifies it. You say that you tried using a temporary variable and it didn't work, have you tried something like this?

    For x = 1 to 8
        tmp = x
        update1 =  SwfWindow("NextGen File Maintenance").SwfWindow("SIM Library Configuration").SwfObject("spreadCtl").Object.ActiveSheet.GetValue(tmp, 2)
        MsgBox("x=" & x & " tmp=" & tmp)
    Next    
    

    If the problem is that GetValue indeed changes the index you can try using a function that takes the index by value and then use that in the loop.

    Public Function GetVal(ByVal index)
           GetVal = SwfWindow("NextGen File Maintenance").SwfWindow("SIM Library Configuration").SwfObject("spreadCtl").Object.ActiveSheet.GetValue(index, 2)
    End Function