0

我正在研究一个宏,它将根据我将在“A”列中提供的发票列表从 SAP 获取 PDF。此发票将保存在给定路径“D6”中。

Public Declare PtrSafe Function GetActiveWindow Lib "user32" () As Long
Sub save_invoice()

Dim sInvName As String
Dim sPath As String
Dim WshShell


Set WshShell = CreateObject("WScript.Shell")

lr = Range("A" & Rows.Count).End(xlUp).Row 'count all given invoice numbers
sPath = Range("D6") 'in this cell we provide path in which all PDF's should be saved


If Not IsObject(App) Then
   Set SapGuiAuto = GetObject("SAPGUI")
   Set App = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
   Set Connection = App.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 App, "on"
End If

    session.findById("wnd[0]/tbar[0]/okcd").Text = "/nvf03"
    session.findById("wnd[0]").sendVKey 0

    For i = 1 To lr
            session.findById("wnd[0]/usr/ctxtVBRK-VBELN").Text = Range("A" & i)
            sInvName = Range("A" & i)
            session.findById("wnd[0]/mbar/menu[0]/menu[11]").Select
            session.findById("wnd[1]/usr/tblSAPLVMSGTABCONTROL").getAbsoluteRow(0).Selected = True
            session.findById("wnd[1]/tbar[0]/btn[37]").press
            session.findById("wnd[0]/tbar[0]/okcd").Text = "pdf!"
            session.findById("wnd[0]").sendVKey 0

 'open "PDF Preview" window
        WshShell.AppActivate "PDF Preview"

        Do
        Loop Until WshShell.AppActivate("PDF Preview") = True


        WshShell.SendKeys "{ENTER}"

        Application.Wait (Now + TimeValue("0:00:01"))
        WshShell.SendKeys "{TAB}"
            Application.Wait (Now + TimeValue("0:00:01"))
        WshShell.SendKeys "{TAB}"
            Application.Wait (Now + TimeValue("0:00:01"))
        WshShell.SendKeys "{TAB}"
            Application.Wait (Now + TimeValue("0:00:01"))
        WshShell.SendKeys "^+s" 'Open save dialog
        WshShell.SendKeys "{ENTER}"
'>>>>here should be open "Save As" window
        Do
            WshShell.SendKeys "{TAB}"
                Application.Wait (Now + TimeValue("0:00:01"))
            WshShell.SendKeys "{TAB}"
                Application.Wait (Now + TimeValue("0:00:01"))
            WshShell.SendKeys "^+s"

       Loop Until WshShell.AppActivate("Save As") = True

        Application.Wait (Now + TimeValue("0:00:01"))
        WshShell.SendKeys "%n" 'Save the file
        Application.Wait (Now + TimeValue("0:00:01"))
        WshShell.SendKeys sPath & sInvName & ".pdf" 'Path and file name
        Application.Wait (Now + TimeValue("0:00:01"))
        WshShell.SendKeys "%+s" 'Save the file

            session.findById("wnd[1]").Close
            session.findById("wnd[0]/tbar[0]/btn[3]").press
            session.findById("wnd[1]").Close
    Next i

End Sub''''

“另存为”窗口存在一些问题,没有出现。宏在“PDF 预览”上停止,我想进入下一步“保存副本”,然后会出现“另存为”窗口,但我无法为此提供正确的代码

4

1 回答 1

0

我几乎可以肯定你会在这里找到解决问题的方法:

https://answers.sap.com/questions/12057563/scripts-to-save-a-pdf-copy-of-a-doc-from-sap-scree.html

问候, ScriptMan

于 2019-08-30T08:40:11.257 回答