Ross
Ross

Reputation: 97

Executing string as property to set values in SAP with VBS

I'm trying to automate a list of property set commands in SAP GUI 740, for example, to set the "text" property of a field to "12345" as shown below.

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

Function Overall()
    session.findById("wnd[0]/tbar[0]/okcd").text = "12345"
end function

call Overall

That works fine, as does:

Function Overall()
    set control = session.findById("wnd[0]/tbar[0]/okcd")
    control.text = "12345"
end function

And so does:

Function Overall()
    set control = session.findById("wnd[0]/tbar[0]/okcd")
    with control
        .text = "12345"
    end with
end function

What I need to figure out is how to pass such a function the property name and value as strings and have it set those. For instance:

Function Desired(Input)
    GUI_ID = Input(0)
    Property_to_change = Input(1)
    Value_to_change = Input(2)
    session.findById(GUI_ID).Property_to_change = Value_to_change
end function

The best option seems to be CallByName, such as below, but I get a type mismatch error.

Function Desired(Input)
    GUI_ID = Input(0)
    Property_to_change = Input(1)
    Value_to_change = Input(2)
    set control = session.findById(GUI_ID)
    CallByName control, Property_to_change, vbSet, Value_to_change
end function

And the error:

Microsoft VBScript runtime error: Type mismatch: 'callbyname'

I don't know if this is a simple syntax issue, or if I am using this completely wrong. I'm also not invested in CallByName, so if there is a better or easier way, I'm all for it :)

Thank you everyone!

Upvotes: 0

Views: 1138

Answers (1)

ScriptMan
ScriptMan

Reputation: 1625

In a VB script, the task could be solved as follows.

for example:

Function Desired(Input_0, Input_1, Input_2)
 GUI_ID = Input_0
 Property_to_change = Input_1
 Value_to_change = Input_2
 set control = session.findById(GUI_ID)
 if Property_to_change = "text" then
  with control
    .text = Value_to_change
  end with
  session.findById("wnd[0]").sendVKey 0
 end if
 if Property_to_change = "setFocus" then
  with control
    .setFocus
  end with
 end if 
 'etc.
end function

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
session.findById("wnd[0]").maximize
call Desired("wnd[0]/tbar[0]/okcd", "text", "12345")  

Upvotes: 1

Related Questions