How to run SAP GUI script from Excel Macro

房东的猫 提交于 2020-06-27 13:37:29

问题


I am trying to create a Excel macro which executes the SAP GUI Script. I already created the SAP script but I didn't understand how to use that in VBA macro.

This is my SAP GUI Script :

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 = "ZL"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/chkP_DBAGG").selected = true
session.findById("wnd[0]/usr/ctxtP_DTA").text = "DB"
session.findById("wnd[0]/usr/chkP_DBAGG").setFocus
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]/tbar[1]/btn[25]").press
session.findById("wnd[0]/tbar[1]/btn[26]").press
session.findById("wnd[0]/usr/chkS005").selected = true
session.findById("wnd[0]/usr/chkS017").selected = true
session.findById("wnd[0]/usr/chkS018").selected = true
session.findById("wnd[0]/usr/chkS020").selected = true
session.findById("wnd[0]/usr/chkS025").selected = true
session.findById("wnd[0]/usr/chkS030").selected = true
session.findById("wnd[0]/usr/chkS031").selected = true
session.findById("wnd[0]/usr/chkS055").selected = true
session.findById("wnd[0]/usr/chkS057").selected = true
session.findById("wnd[0]/usr/chkS057").setFocus
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]/usr/ctxtC025-LOW").setFocus
session.findById("wnd[0]/usr/ctxtC025-LOW").caretPosition = 0
session.findById("wnd[0]").sendVKey 4
session.findById("wnd[1]/usr/cntlCONTAINER/shellcont/shell").selectionInterval = "20170717,20170717"
session.findById("wnd[0]/usr/ctxtC025-HIGH").setFocus
session.findById("wnd[0]/usr/ctxtC025-HIGH").caretPosition = 0
session.findById("wnd[0]").sendVKey 4
session.findById("wnd[1]/usr/cntlCONTAINER/shellcont/shell").focusDate = "20170724"
session.findById("wnd[1]/usr/cntlCONTAINER/shellcont/shell").selectionInterval = "20170724,20170724"
session.findById("wnd[0]/usr/txtL_MX").text = "9999999"
session.findById("wnd[0]/usr/txtL_MX").setFocus
session.findById("wnd[0]/usr/txtL_MX").caretPosition = 11
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]/mbar/menu[0]/menu[3]/menu[1]").select
session.findById("wnd[1]/usr/ctxtDY_PATH").setFocus
session.findById("wnd[1]/usr/ctxtDY_PATH").caretPosition = 0
session.findById("wnd[1]").sendVKey 4
session.findById("wnd[2]/usr/ctxtDY_PATH").setFocus
session.findById("wnd[2]/usr/ctxtDY_PATH").caretPosition = 0
session.findById("wnd[2]").sendVKey 4
session.findById("wnd[3]/usr/ctxtDY_PATH").setFocus
session.findById("wnd[3]/usr/ctxtDY_PATH").caretPosition = 0
session.findById("wnd[3]").sendVKey 4
session.findById("wnd[4]/usr/ctxtDY_PATH").text = "G:\PROFILES\AP\Desktop"
session.findById("wnd[4]/usr/ctxtDY_FILENAME").text = "report.xlsx"
session.findById("wnd[4]/usr/ctxtDY_FILENAME").caretPosition = 11
session.findById("wnd[4]/tbar[0]/btn[11]").press
session.findById("wnd[3]/tbar[0]/btn[11]").press
session.findById("wnd[2]/tbar[0]/btn[0]").press
session.findById("wnd[1]/tbar[0]/btn[11]").press

I have few doubts regarding this:

  1. How can I add this in Excel macro.
  2. Do I need to open the SAP manually before running it?
  3. Is there any ad-on code do I need to use to open the SAP from Excel macro?

I only need button click to perform the process. I didn't pass any value dynamically.


回答1:


Open and log in to SAP manually.

Open Excel, make sure the developer tab is visible. If not then select;

File/Options/Customize Ribbon. Make sure 'Developer' is ticked.

Select the Developer tab in Excel then choose 'Insert'. From the 'Active X' set of controls (Not the 'FORM' controls') select the Command Button control and draw it on your worksheet. Right-click the button and select 'Properties', look for 'Caption' and either delete it or change it to something more appropriate. After that, right-click the command button again and select 'View Code'. Paste the following code into the vba editor.

On the VBA editor menu select; Debug/Compile VBA Project. Hopefully there will be no errors and you can now close the VBA editor.

Now, click your button and the code should run your SAP transaction. If the button won't click, make sure 'Design Mode' isn't selected in the Developer tab.

Don't forget to save your spreadsheet :)

Private Sub CommandButton1_Click()

On Error GoTo Err_NoSAP

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(SAP_session) Then
   Set SAP_session = Connection.Children(0)
End If
If IsObject(WScript) Then
   WScript.ConnectObject SAP_session, "on"
   WScript.ConnectObject SAPGuiApp, "on"
End If

If (Connection.Children.Count > 1) Then GoTo Err_TooManySAP

Set aw = SAP_session.ActiveWindow()
aw.findById("wnd[0]").Maximize

On Error GoTo Err_Description
SAP_session.findById("wnd[0]").Maximize
SAP_session.findById("wnd[0]/tbar[0]/okcd").Text = "ZL"
SAP_session.findById("wnd[0]").sendVKey 0
SAP_session.findById("wnd[0]/usr/chkP_DBAGG").Selected = True
SAP_session.findById("wnd[0]/usr/ctxtP_DTA").Text = "DB"
SAP_session.findById("wnd[0]/usr/chkP_DBAGG").SetFocus
SAP_session.findById("wnd[0]/tbar[1]/btn[8]").press
SAP_session.findById("wnd[0]/tbar[1]/btn[25]").press
SAP_session.findById("wnd[0]/tbar[1]/btn[26]").press
SAP_session.findById("wnd[0]/usr/chkS005").Selected = True
SAP_session.findById("wnd[0]/usr/chkS017").Selected = True
SAP_session.findById("wnd[0]/usr/chkS018").Selected = True
SAP_session.findById("wnd[0]/usr/chkS020").Selected = True
SAP_session.findById("wnd[0]/usr/chkS025").Selected = True
SAP_session.findById("wnd[0]/usr/chkS030").Selected = True
SAP_session.findById("wnd[0]/usr/chkS031").Selected = True
SAP_session.findById("wnd[0]/usr/chkS055").Selected = True
SAP_session.findById("wnd[0]/usr/chkS057").Selected = True
SAP_session.findById("wnd[0]/usr/chkS057").SetFocus
SAP_session.findById("wnd[0]/tbar[1]/btn[8]").press
SAP_session.findById("wnd[0]/usr/ctxtC025-LOW").SetFocus
SAP_session.findById("wnd[0]/usr/ctxtC025-LOW").caretPosition = 0
SAP_session.findById("wnd[0]").sendVKey 4
SAP_session.findById("wnd[1]/usr/cntlCONTAINER/shellcont/shell").selectionInterval = "20170717,20170717"
SAP_session.findById("wnd[0]/usr/ctxtC025-HIGH").SetFocus
SAP_session.findById("wnd[0]/usr/ctxtC025-HIGH").caretPosition = 0
SAP_session.findById("wnd[0]").sendVKey 4
SAP_session.findById("wnd[1]/usr/cntlCONTAINER/shellcont/shell").focusDate = "20170724"
SAP_session.findById("wnd[1]/usr/cntlCONTAINER/shellcont/shell").selectionInterval = "20170724,20170724"
SAP_session.findById("wnd[0]/usr/txtL_MX").Text = "9999999"
SAP_session.findById("wnd[0]/usr/txtL_MX").SetFocus
SAP_session.findById("wnd[0]/usr/txtL_MX").caretPosition = 11
SAP_session.findById("wnd[0]/tbar[1]/btn[8]").press
SAP_session.findById("wnd[0]/mbar/menu[0]/menu[3]/menu[1]").Select
SAP_session.findById("wnd[1]/usr/ctxtDY_PATH").SetFocus
SAP_session.findById("wnd[1]/usr/ctxtDY_PATH").caretPosition = 0
SAP_session.findById("wnd[1]").sendVKey 4
SAP_session.findById("wnd[2]/usr/ctxtDY_PATH").SetFocus
SAP_session.findById("wnd[2]/usr/ctxtDY_PATH").caretPosition = 0
SAP_session.findById("wnd[2]").sendVKey 4
SAP_session.findById("wnd[3]/usr/ctxtDY_PATH").SetFocus
SAP_session.findById("wnd[3]/usr/ctxtDY_PATH").caretPosition = 0
SAP_session.findById("wnd[3]").sendVKey 4
SAP_session.findById("wnd[4]/usr/ctxtDY_PATH").Text = "G:\PROFILES\AP\Desktop"
SAP_session.findById("wnd[4]/usr/ctxtDY_FILENAME").Text = "report.xlsx"
SAP_session.findById("wnd[4]/usr/ctxtDY_FILENAME").caretPosition = 11
SAP_session.findById("wnd[4]/tbar[0]/btn[11]").press
SAP_session.findById("wnd[3]/tbar[0]/btn[11]").press
SAP_session.findById("wnd[2]/tbar[0]/btn[0]").press
SAP_session.findById("wnd[1]/tbar[0]/btn[11]").press

Exit Sub

Err_Description:
    MsgBox ("The program has generated an error;" & Chr(13) & _
    "the reason for this error is unknown."), VbInformation, _
    "For Information..."
        Exit Sub

Err_NoSAP:
MsgBox ("You don't have SAP open or " & Chr(13) & _
"scripting has been disabled."), VbInformation, _
"For Information..."
        Exit Sub

Err_TooManySAP:
MsgBox ("You must only have one SAP session open. " & Chr(13) & _
        "Please close all other open SAP sessions."), VbInformation, _
        "For Information..."
         Exit Sub

End Sub



回答2:


Unfortunately, I no longer have access to any SAP applications due to changing jobs so the below is from what I remember.

  • You can add this into an Excel macro by copy and pasting into a Sub.

  • You will need to open SAP manually as well keeping the computer unlocked, otherwise your macro will crash. It might be possible to open SAP via the macro, I did it in my last job and logged into it with a hard-coded password but I don't have the code to share.

  • In the script pasted, replace every instance of application with something else, such as guiApplication, otherwise Excel will get confused with its own Excel.Application member.

  • No other code is required but you will need to call the Subroutine and ensure you do not have option explicit set. You can but then you will need to instantiate session, connection, etc.

Where to put the script:

 Public Sub MySapScript()
     ' Your Sap Script here
 End Sub



回答3:


I always had problems using the connection script that the recorder spit out (If IsObject(WScript) Then).

This is what I've always used. In a code module insert the below.

Sub Connect_To_SAP()

    Dim SapGuiAuto As Object
    Dim SAPApp As Object
    Dim SAPCon As Object
    Dim session As Object

    Set SapGuiAuto = GetObject("SAPGUI")
    Set SAPApp = SapGuiAuto.GetScriptingEngine
    Set SAPCon = SAPApp.Children(0)
    Set session = SAPCon.Children.ElementAt(0) ' <--- Assumes you are using the first session open. '

'Your script here

End Sub

You can always throw your recorded script in here, but when you want to take things to the next level you should try adding the SAP GUI Scripting API. Deep in the SAP folder of your program files there's a class library you can use to make your SAP automation flawless. Try searching for this file "sapfewse.ocx". Get the file path.

Then, in the IDE select Tools > References then click Browse and select that file. Once you've added it to the VBA Project press F2 to view all the methods and properties at your disposal.



来源:https://stackoverflow.com/questions/45187903/how-to-run-sap-gui-script-from-excel-macro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!