将字符串执行为 属性 以使用 VBS 在 SAP 中设置值
Executing string as property to set values in SAP with VBS
我正在尝试在 SAP GUI 740 中自动执行一系列 属性 set 命令,例如,将字段的 "text" 属性 设置为“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,所以如果有更好或更简单的方法,我全力以赴:)
谢谢大家!
在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")
我正在尝试在 SAP GUI 740 中自动执行一系列 属性 set 命令,例如,将字段的 "text" 属性 设置为“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,所以如果有更好或更简单的方法,我全力以赴:)
谢谢大家!
在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")