如何从SAP GUI提取PDF文档? (脚本)

时间:2019-09-02 17:32:03

标签: vba scripting sap sap-gui

我正在尝试从SAP提取PDF文档,但是,在第二部分中,我需要启动“另存为”序列。

代码的第一部分涉及连接到SAP并查询特定的发票编号,然后是几行代码,我试图将其附加到包含PDF文档的第二个SAP窗口中。诀窍是使用发送密钥保存实际文件。

Sub DocumentExtraction()

    Dim varResponse As Variant 'Prompts the user if macro should be run.
        varResponse = MsgBox("Are you sure you wish to run extraction macro?", vbYesNo, "Warning!")
        If varResponse <> vbYes Then Exit Sub

    Set SapGuiAuto = GetObject("SAPGUI") 'Obtains SAP GUI Scripting object.
    Set SAPApp = SapGuiAuto.GetScriptingEngine 'Obtains currently running SAP GUI.
    Set SAPCon = SAPApp.Children(0) 'Obtains the first system currently connected.
    Set session = SAPCon.Children(0) 'Obtains the first session (window) on that connection.

    ' SAP scripting starts here.

    session.findById("wnd[0]").maximize
    session.findById("wnd[0]/tbar[0]/okcd").Text = "/nFB03"
    session.findById("wnd[0]").sendVKey 0
    session.findById("wnd[0]/usr/txtRF05L-BELNR").Text = "930502016"
    session.findById("wnd[0]/usr/ctxtRF05L-BUKRS").Text = "7360"
    session.findById("wnd[0]/usr/txtRF05L-GJAHR").Text = "2019"
    session.findById("wnd[0]/usr/txtRF05L-GJAHR").SetFocus
    session.findById("wnd[0]/usr/txtRF05L-GJAHR").caretPosition = 4
    session.findById("wnd[0]").sendVKey 0
    session.findById("wnd[0]/titl/shellcont/shell").pressContextButton "%GOS_TOOLBOX"
    session.findById("wnd[0]/titl/shellcont/shell").selectContextMenuItem "%GOS_VIEW_ATTA"
    session.findById("wnd[1]/usr/cntlCONTAINER_0100/shellcont/shell").selectedRows = "0"
    session.findById("wnd[1]/usr/cntlCONTAINER_0100/shellcont/shell").doubleClickCurrentCell

    Set session = SAPCon.Children(1) 'Obtains the second session (window) on current connection.

    session.findById("wnd[0]/shellcont/shell/shellcont[1]/shell").SetFocus

End Sub

我在SAP支持页面上找到了类似的帖子,但是,建议的解决方案似乎不起作用-对象不存在。

1 个答案:

答案 0 :(得分:-1)

一个简单的解决方法是,通过导航到Internet Explorer /工具/ Internet选项/程序/管理加载项/选择所有加载项/禁用Adobe PDF Reader,在Internet Explorer中禁用Adobe PDF Reader。

然后在单击文档后使用下面的代码,它将打开打开/保存/关闭窗口。

Application.Wait Now + TimeValue("0:00:15")
SendKeys "%s" 'ALT + S initiates save as procedure.

Application.Wait Now + TimeValue("0:00:05")
SendKeys ExtractFolderPath & ExtractFileName 'File name and path are entered.

Application.Wait Now + TimeValue("0:00:02")
SendKeys "{ENTER}"

Application.Wait Now + TimeValue("0:00:02")
SendKeys "{F3}" 'Closes Document Viewer window.

Application.Wait Now + TimeValue("0:00:02")
session.findById("wnd[1]").Close 'Closes Attachment List window.