Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SAP宏在Excel中运行,如何在Excel工作表中填充数据?_Excel_Vba_Sap Gui - Fatal编程技术网

SAP宏在Excel中运行,如何在Excel工作表中填充数据?

SAP宏在Excel中运行,如何在Excel工作表中填充数据?,excel,vba,sap-gui,Excel,Vba,Sap Gui,我创建了一个SAP宏,用于获取我需要的数据: 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

我创建了一个SAP宏,用于获取我需要的数据:

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
session.findById("wnd[0]/tbar[0]/okcd").text = "ZPRS"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtP_PLANT").text = "1707"
session.findById("wnd[0]/usr/ctxtS_ARBPL-LOW").text = "BSLSR"
session.findById("wnd[0]/usr/ctxtS_ARBPL-LOW").setFocus
session.findById("wnd[0]/usr/ctxtS_ARBPL-LOW").caretPosition = 5
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]/tbar[1]/btn[8]").press
Private Sub CommandButton1_Click()
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 SAPGuiApp, "on"
End If
session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").Text = "ZPRS"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtP_PLANT").Text = "1707"
session.findById("wnd[0]/usr/ctxtS_ARBPL-LOW").Text = "BSLSR"
session.findById("wnd[0]/usr/ctxtS_ARBPL-LOW").SetFocus
session.findById("wnd[0]/usr/ctxtS_ARBPL-LOW").caretPosition = 5
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]/tbar[1]/btn[8]").press

End Sub
然后我接下来做的是通过Excel制作一个按钮,以获取我需要的数据:

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
session.findById("wnd[0]/tbar[0]/okcd").text = "ZPRS"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtP_PLANT").text = "1707"
session.findById("wnd[0]/usr/ctxtS_ARBPL-LOW").text = "BSLSR"
session.findById("wnd[0]/usr/ctxtS_ARBPL-LOW").setFocus
session.findById("wnd[0]/usr/ctxtS_ARBPL-LOW").caretPosition = 5
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]/tbar[1]/btn[8]").press
Private Sub CommandButton1_Click()
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 SAPGuiApp, "on"
End If
session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").Text = "ZPRS"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtP_PLANT").Text = "1707"
session.findById("wnd[0]/usr/ctxtS_ARBPL-LOW").Text = "BSLSR"
session.findById("wnd[0]/usr/ctxtS_ARBPL-LOW").SetFocus
session.findById("wnd[0]/usr/ctxtS_ARBPL-LOW").caretPosition = 5
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]/tbar[1]/btn[8]").press

End Sub

运行脚本后,一切正常。但是,我遇到的问题是,excel宏每次都会创建一个新的excel工作表,我希望它填充到按钮所在的excel工作表中。我真的不知道如何使用它,我想知道是否有人对此有所了解?

您的SAP应用程序生成了一个Excel文件,您希望您的VBA宏复制此工作表中的数据,并将其粘贴到包含VBA宏的Excel文件的工作表中。我认为您的VBA无法告诉SAP应用程序将数据直接放在给定的位置。请解释一下您的SAP应用程序“每次都在创建一个新的Excel工作表”,以便我们能够帮助您。我已经解决了这个问题。最后,我将其保存到所有现有工作簿桌面,并将保存的目标文件夹重新格式化为excel文件所在的位置,从宏运行的位置到特定文件夹。然后,我使用power query editor创建数据连接。