问题描述
如果我想使用Excel VBA从NWBC启动的SAP实例中收集信息,我如何/可以设法做到这一点?如果我通过Citrix连接启动NWBC SAP,并通过NWBC中的Excel实例启动代码,则代码运行良好...
Sub Start_SAP_things()
If Not IsObject(Application) Then
Set SapGuiAuto = GetObject("SAPGUISERVER")
Set SAPApp = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = SAPApp.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
'And so on
End Sub
但是是否有可能使用连接到NWBC SAP的“ desktop-excel”中的Excel宏文件? (现在很不幸,我已经创建了一个导出表单SAP(例如ALV-Grid),以便拥有一个活动的NWBC-Excel实例,然后调用该过程……非常讨厌)
最好的问候和预先的感谢。
众议院MD
解决方法
如果我对您的理解正确,则希望从EXCEL运行SAP脚本。如果是这样,那么这就是您所需要的:
Option Explicit
Private Sub CommandButton1_Click()
On Error GoTo Err_NoSAP
Dim SAPguiApp As Variant
Dim SapGuiAuto As Object
Dim Connection As Variant
Dim Session As Variant
Dim WScript As Variant
Dim aw As Object
If Not IsObject(SAPguiApp) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set SAPguiApp = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = SAPguiApp.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
If (Connection.Children.Count > 1) Then GoTo Err_TooManySAP
Set aw = Session.ActiveWindow()
aw.findById("wnd[0]").maximize
On Error GoTo Err_Description
'Your SAP script code here:
'====================================================================================================================================
'Example code - replace with yours.
Session.findById("wnd[0]/tbar[0]/okcd").Text = "IW38"
Session.findById("wnd[0]").sendVKey 0
'====================================================================================================================================
Exit Sub
Err_NoSAP:
MsgBox ("You don't have SAP open or scripting has been disabled."),vbInformation,"For Information..."
Exit Sub
Err_TooManySAP:
MsgBox "You can only have one SAP session open." & Chr(13) & Chr(13) & _
"Please close all other open SAP sessions.","For Information..."
Exit Sub
Err_Description:
MsgBox "The program has generated an error,the reason for this error is unknown." & Chr(13) & Chr(13) & _
"If the error persists,try closing and re-opening SAP." & Chr(13) & Chr(13) & _
"Microsoft's definition for the error is: " & Err.Number & " - " & Err.Description & ".","For Information..."
End Sub