本文介绍了如何从Excel宏运行SAP GUI脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个执行SAP GUI脚本的Excel宏.我已经创建了SAP脚本,但不了解如何在VBA宏中使用它.

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.

这是我的SAP GUI脚本:

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

对此我几乎没有疑问:

  1. 如何在Excel宏中添加它.
  2. 我需要在运行之前手动打开SAP吗?
  3. 是否需要使用任何附加代码才能从Excel宏打开SAP?

我只需要单击按钮即可执行该过程.我没有动态传递任何值.

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

推荐答案

打开并手动登录到SAP.

Open and log in to SAP manually.

打开Excel,确保开发人员"选项卡可见.如果没有,请选择;

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

文件/选项/自定义功能区.确保选中开发人员".

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

在Excel中选择开发人员"选项卡,然后选择插入".从"Active X"集中控件(而不是"FORM"控件)中,选择命令按钮"控件并将其绘制在工作表上.右键单击该按钮,然后选择属性",查找标题",然后将其删除或将其更改为更合适的名称.之后,再次右键单击命令按钮,然后选择查看代码".将以下代码粘贴到vba编辑器中.

Select the Developer tab in Excel then choose 'Insert'. From the 'Active X' setof 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.

在VBA编辑器菜单上选择;调试/编译VBA项目.希望不会有错误,您现在可以关闭VBA编辑器.

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

现在,单击您的按钮,该代码应运行您的SAP事务.如果该按钮不会单击,请确保在开发人员"标签中未选择设计模式".

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

这篇关于如何从Excel宏运行SAP GUI脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 22:38
查看更多