cancel
Showing results for 
Search instead for 
Did you mean: 

Export print preview PDF from SAP using VBA

0 Kudos

Hi everyone,

I have the following situation: I need to export from SAP 750 the invoices from VF02 but i am not able to do it. I wrote the below code, which is straightforward but it is simply not working. The VBA dosen't provide any error and finish the task but here is no output. The line that should open the the save dialog do nothing:

Public Sub ExportInvoices()
'ByVal vStrPathSAP As String
    Dim CC, BillingDoc As String
    Dim vcounter As Integer

    'set condition to start loop until active cell become blank
    ThisWorkbook.Activate
    Sheets("Sheet1").Select
    Range("A1").Select
    Do While ActiveCell.Value <> ""
    
    'establish active row
    Range("A1").Select
    vcounter = ActiveCell.Row
    
    Set xclapp = GetObject(, "Excel.Application")
    Set xclwbk = xclapp.ThisWorkbook
    Set xclsht = xclwbk.Sheets("Sheet1")
    BillingDoc = xclsht.Cells(vcounter, 9).Value
    CC = xclsht.Cells(vcounter, 2).Value
    
    Dim App, Connection, session As Object
    Set SapGuiAuto = GetObject("SAPGUI")
    Set App = SapGuiAuto.GetScriptingEngine
    Set Connection = App.Children(0)
    Set session = Connection.Children(0)

            session.findById("wnd[0]").maximize
            session.findById("wnd[0]/tbar[0]/okcd").Text = "/nVF02"
            session.findById("wnd[0]").sendVKey 0
            session.findById("wnd[0]/usr/ctxtVBRK-VBELN").Text = BillingDoc
            session.findById("wnd[0]/usr/ctxtRV60S-BUKRS").Text = CC
            session.findById("wnd[0]/usr/ctxtRV60S-BUKRS").SetFocus
            session.findById("wnd[0]/usr/ctxtRV60S-BUKRS").caretPosition = 4
            session.findById("wnd[0]/mbar/menu[0]/menu[11]").Select
            session.findById("wnd[1]/tbar[0]/btn[37]").press
       
--->>> nothing happend from here
            Set WshShell = CreateObject("WScript.Shell")
            WshShell.AppActivate "Print Preview, Document 1 of 1"


            'set the focus to the Acrobat control based on ID of the control
            session.findById("wnd[0]/usr/cntlHTML_IFBA_PREVIEW/shellcont/shell").SetFocus


            WshShell.SendKeys "^+s" 'Open save dialog


            WshShell.SendKeys "%n" 'Activate file name field
            WshShell.SendKeys "\\Invoices under investigation\" & Excel.Cells(vcounter, "B") & "\" & Excel.Cells(vcounter, "I") & ".pdf" 'Path and file name
            WshShell.SendKeys "%s" 'Save the file
       
       
            'go to next row
            ActiveCell.Offset(1, 0).Activate
            vcounter = ActiveCell.Row
    Loop
    
End Sub


script_man
Active Contributor
0 Kudos

Please take a look at the following link and especially the article by Michal Udvardy:

https://answers.sap.com/questions/461898/hello-i-am-new-to-scripting-specially-to-copy-sap.html

Accepted Solutions (0)

Answers (1)

Answers (1)

0 Kudos

I have the same solution. did you do it?