Search code examples
vbapowershellsap-guiwscript.shell

Use AppActivate to Save As


I'm working on a macro which will get PDF from SAP according to of invoice list which I will provide in column "A". This invoices will be saved in the given path "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''''

There is some problem with "Save as" window which does not appear. Macro stopped on "PDF Preview" and I would like to go to next step to "Save a copy" and then "Save as" window will be appeared, but I can't provide correct code for that


Solution

  • I'm almost sure you'll find the solution to your problem here:

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

    Regards, ScriptMan