0

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!

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Ross
  • 97
  • 4
  • `CallByName` is in VBA, not VBS, isn't it? – Sandra Rossi Jun 13 '20 at 21:08
  • I have very little experience with VBA and VBS, so that would explain why it isn't working :P So the question remains ... is this evaluation of a string as a property possible in VBS? Or even a string as an entire command, e.g. `"control.text = ""12345"""`. `Eval(string)` works for something like `"control.setFocus"`, but not for setting property values. Thoughts? Suggestions? Thanks! – Ross Jun 13 '20 at 23:01

1 Answers1

1

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")  
Geert Bellekens
  • 12,788
  • 2
  • 23
  • 50
ScriptMan
  • 1,580
  • 1
  • 9
  • 9