VBA无法正常运行

时间:2018-08-22 22:38:16

标签: vba sap

因此,我的任务是通过从电子表格中提取值,将其插入SAP表单并从SAP事务中获取值来将Excel与SAP连接起来。

我认为我只需要为此运行VBA文件。运行代码时,电子表格已打开,并且已登录到SAP。我仍然无法弄清楚为什么代码不起作用。请帮忙。

select '2018-01-01',count(1) from prd_fct.mktng where process_dt='2018-01-01' union all
select '2018-01-02',count(1) from prd_fct.mktng where process_dt='2018-01-02' 

1 个答案:

答案 0 :(得分:0)

感谢您的反馈。原来,我需要在运行脚本之前在Excel中声明SAP对象。这是可以在Excel中使用的脚本。

Dim Application, Connection, Session As Object
Set SapGuiAuto = GetObject("SAPGUI")
Set Application = SapGuiAuto.GetScriptingEngine
Set Connection = Application.Children(0)
Set Session = Connection.Children(0)

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

Dim objExcel As Object
Dim objSheet As Object

Set objExcel = GetObject(, "Excel.Application")
Set objSheet = objExcel.ActiveWorkbook.ActiveSheet

COL1 = Trim(CStr(objSheet.Range("A1").Value)) 'Column1

Session.findById("wnd[0]/tbar[0]/okcd").Text = "/nCS15"
Session.findById("wnd[0]/tbar[0]/btn[0]").press

Session.findById("wnd[0]/usr/ctxtRC29L-MATNR").Text = COL1
Session.findById("wnd[0]/usr/ctxtRC29L-MATNR").SetFocus