首页 > 解决方案 > 使用 VBS 执行字符串作为属性在 SAP 中设置值

问题描述

我正在尝试自动化 SAP GUI 740 中的属性设置命令列表,例如,将字段的“文本”属性设置为“12345”,如下所示。

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

这很好用,如下所示:

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

也是如此:

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

我需要弄清楚的是如何将属性名称和值作为字符串传递这样的函数并让它设置它们。例如:

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

最好的选择似乎是 CallByName,如下所示,但我收到类型不匹配错误。

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

和错误:

Microsoft VBScript runtime error: Type mismatch: 'callbyname'

我不知道这是否是一个简单的语法问题,或者我是否完全错误地使用了它。我也没有投资 CallByName,所以如果有更好或更简单的方法,我完全赞成 :)

谢谢大家!

标签: vbscriptsap-guicallbyname

解决方案


在 VB 脚本中,该任务可以解决如下。

例如:

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")  

推荐阅读