Where variable declared changes behavior

PJ_in_FL
5StarLounger
Posts: 1100
Joined: 21 Jan 2011, 16:51
Location: Florida

Where variable declared changes behavior

Post by PJ_in_FL »

I'm not a total novice with VBA and VBScript, but I'm totally stumped and hope the great minds here don't mind helping!

I use SAP at work and automate various tasks via the scripting interface using Excel/VBA and VBScript. I ran into a problem with a VBScript program I was working on that looked just like older VBScript programs that worked.

With multiple SAP sessions open, here's a simple script that works when executed by drag and drop onto one of the sessions. Note all the declarations are commented out so the objects are "created" when the script executes the SET statements.

Code: Select all

'Option Explicit
'Dim SapGuiAuto,  application, connection, session, WScript

main   

sub main
  OPEN_SESSION
end sub

sub  OPEN_SESSION
'Dim SapGuiAuto,  application, connection, session, WScript

    If Not IsObject(application) Then
       Set SapGuiAuto  = GetObject("SAPGUI")
       Set application = SapGuiAuto.GetScriptingEngine
    End If
    If Not IsObject(connection) Then
       Set connection = application.Children(0)
    End If
    If Not IsObject(session) Then
       Set session    = connection.Children(0)
    End If
    If IsObject(WScript) Then
       WScript.ConnectObject session,     "on"
       WScript.ConnectObject application, "on"
    End If

MsgBox session.findById("wnd[0]/titl").text

end sub
Displaying the title of the session tells which of the sessions is linked to the "session" object. This version links to the session the script was dropped onto.

If any of the other methods of declaring the objects are used, by uncommenting the lines commented out above, the session links to the first session opened, not the drop target.

Here's an example that fails:

Code: Select all

Option Explicit
'Dim SapGuiAuto,  application, connection, session, WScript

main   

sub main
  OPEN_SESSION
end sub

sub  OPEN_SESSION
Dim SapGuiAuto,  application, connection, session, WScript

    If Not IsObject(application) Then
       Set SapGuiAuto  = GetObject("SAPGUI")
       Set application = SapGuiAuto.GetScriptingEngine
    End If
    If Not IsObject(connection) Then
       Set connection = application.Children(0)
    End If
    If Not IsObject(session) Then
       Set session    = connection.Children(0)
    End If
    If IsObject(WScript) Then
       WScript.ConnectObject session,     "on"
       WScript.ConnectObject application, "on"
    End If

MsgBox session.findById("wnd[0]/titl").text

end sub


So my question is why does declaring the object variables, either globally or locally, change the session the code links to?
PJ in (usually sunny) FL

Kyle123
Lounger
Posts: 48
Joined: 15 Nov 2012, 12:22

Re: Where variable declared changes behavior

Post by Kyle123 »

WARNING: Complete speculation :grin:

I've never used SAP so I don't really know how it works, when you drop a script into it, is that script executed by SAP? If so, at a guess it has already created objects named the same as you have declared. It's possible that by "re-Dimming" them, you're setting them to nothing so your script IsObjects return false and you are re-creating the objects - this would create a new session / first session.

Your first script doesn't re-dimension your variables so SAP is using them "as is" so IsObject() returns true and the already created objects (from before dropping the script) are used in

Code: Select all

session.findById("wnd[0]/titl").text

PJ_in_FL
5StarLounger
Posts: 1100
Joined: 21 Jan 2011, 16:51
Location: Florida

Re: Where variable declared changes behavior

Post by PJ_in_FL »

Kyle,

Thank you for taking time to read and respond!

Your instincts are good! I tried the following test code ...

Code: Select all

MsgBox "Before DIM  --  " & session.findById("wnd[0]/titl").text

main   

sub main
  OPEN_SESSION
end sub

sub  OPEN_SESSION
Dim SapGuiAuto,  application, connection, session, WScript

    If Not IsObject(application) Then
       Set SapGuiAuto  = GetObject("SAPGUI")
       Set application = SapGuiAuto.GetScriptingEngine
    End If
    If Not IsObject(connection) Then
       Set connection = application.Children(0)
    End If
    If Not IsObject(session) Then
       Set session    = connection.Children(0)
    End If
    If IsObject(WScript) Then
       WScript.ConnectObject session,     "on"
       WScript.ConnectObject application, "on"
    End If

MsgBox "After DIM  --  " & session.findById("wnd[0]/titl").text

end sub
... and the first message was from the drop target session while the second message was from the first session opened!

My Excel VBA code usually connects then forces a new session as the working session. I'll need to restructure my VBScript routines to use a similar procedure.

Thanks again!
PJ in (usually sunny) FL

Kyle123
Lounger
Posts: 48
Joined: 15 Nov 2012, 12:22

Re: Where variable declared changes behavior

Post by Kyle123 »

Glad it's working :)