cancel
Showing results for 
Search instead for 
Did you mean: 

Scripts to save a PDF copy of a doc from SAP screen to our desktop.

Former Member

Hi All,

I am trying to save PDF copy of invoice from VF03 screen in SAP to my desktop.

When i record the scripts for the navigation which i do...it records the script till the print preview screen as attached and i am not able to proceed further to save it in my desktop.

Can you please help me get the VB scripts until it saves it in desktop.

I appreciate your help your quick help.

Thanks,

Karthik

Accepted Solutions (1)

Accepted Solutions (1)

stefan_schnell
Active Contributor
0 Kudos

Hello Karthik,

welcome in the Scripting Language forum.

Try the following code:

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(2)
End If

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.AppActivate "Print Preview" 'Title of the window
WScript.Sleep 500

'Set the focus to the Acrobat control but check the correct ID of the control
session.findById("wnd[0]/shellcont/shell/shellcont[1]/shell").SetFocus
WScript.Sleep 250

WshShell.SendKeys "^+s" 'Open save dialog
WScript.Sleep 750
WshShell.SendKeys "%n" 'Activate file name field
WshShell.SendKeys "C:\Users\Public\Desktop\001.pdf" 'Path and file name
WshShell.SendKeys "%s" 'Save the file

You can save your PDF via SendKeys command. At first, after focussing the Acrobat control, open with Ctrl+S the save dialog. Then activate the name field with Alt+N, put the file name in it and save the file via Alt+S.

Let us know your results.

Cheers

Stefan

Former Member
0 Kudos

Hi Stefan,

Thanks for your time and assistance on this!!!

I did try the below codes as per your advise....but it is not taking me to the save page option itself.

Below is the code which i tried along with codes you have given....also i am sure about how to get the correct ID of the control as mentioned by you in the following note " Set the focus to the Acrobat control but check the correct ID of the control " 

Please assist to tweak the code to get the pdf copy saved on the desktop automatically...i really appreciate your help!!! Thanks a ton...Karthik

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 = "/nvf03"

session.findById("wnd[0]").sendVKey 0

session.findById("wnd[0]/usr/ctxtVBRK-VBELN").text = "5100078962"

session.findById("wnd[0]/mbar/menu[0]/menu[11]").select

session.findById("wnd[1]").sendVKey 37

Set WshShell = WScript.CreateObject("WScript.Shell")

WshShell.AppActivate "Print Preview" 'Title of the window

WScript.Sleep 500

'Set the focus to the Acrobat control but check the correct ID of the control

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

WScript.Sleep 250

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

WScript.Sleep 750

WshShell.SendKeys "%n" 'Activate file name field

WshShell.SendKeys "C:\Users\Public\Desktop\001.pdf" 'Path and file name

WshShell.SendKeys "%s" 'Save the file

stefan_schnell
Active Contributor
0 Kudos

Hello Karthik,

I don't have access to VF03 transaction, so I don't know the correct ID of the Acrobat control. You can detect the ID via Scripting Tracker:

To get access to an Acrobat control I use ArchiveLink Document Viewer. In this case the script works and the PDF file is stored in the desktop directory.

Let us know your results.

Cheers

Stefan

Former Member
0 Kudos

Thanks a mill Stefan...i could find the control id and i am able save now on desktop directly.

I just wanna know how do i save it with actual invoice numbers which i can save it in a excel file.

For eg...i have a list of invoice numbers in a excel sheet and i want these scripts to run by picking up each invoice numbers from excel and save it under the same number as file name....continue the same process for rest of it.

I just have scripts until it picks up invoice numbers from excel as below. I want your help to give me scripts to save it under the same invoice number as file name.

****************************************

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 = "/nvf03"

session.findById("wnd[0]").sendVKey 0

Set xclapp = GetObject(, "Excel.Application")

    Set xclwbk = xclapp.Workbooks.Open("C:\Users\mohank\Desktop\Invoice Saving Attempt.xlsx")

    Set xclsht = xclwbk.Sheets("Sheet1")

    For i = 2 To xclapp.ActiveCell.SpecialCells(11).Row

        Transaction = xclsht.Cells(i, 1).Value

session.findById("wnd[0]/mbar/menu[0]/menu[11]").select

session.findById("wnd[1]").sendVKey 37

Set WshShell = WScript.CreateObject("WScript.Shell")

WshShell.AppActivate "Print Preview" 'Title of the window

WScript.Sleep 500

'Set the focus to the Acrobat control but check the correct ID of the control

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

WScript.Sleep 250

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

WScript.Sleep 750

WshShell.SendKeys "%n" 'Activate file name field

WshShell.SendKeys "C:\Users\Public\Desktop\001.pdf" 'Path and file name  'need help here to save it with same invoice number

WshShell.SendKeys "%s" 'Save the file

***************************************

I really appreciate your help so far!!!!!

Thanks,

Karthik

stefan_schnell
Active Contributor
0 Kudos

Hello Karthik,

thanks for your reply.

Here a way how you can flexibilise the file name:

Set xclapp = GetObject(, "Excel.Application")

Set xclwbk = xclapp.Workbooks.Open("C:\Users\mohank\Desktop\Invoice Saving Attempt.xlsx")

Set xclsht = xclwbk.Sheets("Sheet1")

For i = 2 To xclapp.ActiveCell.SpecialCells(11).Row


  'I assume this is the invoice number

  Transaction = xclsht.Cells(i, 1).Value


  session.findById("wnd[0]/mbar/menu[0]/menu[11]").select

  session.findById("wnd[1]").sendVKey 37

  Set WshShell = WScript.CreateObject("WScript.Shell")

  WshShell.AppActivate "Print Preview"

  WScript.Sleep 500

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

  WScript.Sleep 250

  WshShell.SendKeys "^+s"

  WScript.Sleep 750

  WshShell.SendKeys "%n"


  WshShell.SendKeys "C:\Users\Public\Desktop\" & _

    CStr(Transaction) & ".pdf"


  WshShell.SendKeys "%s"

  WScript.Sleep 500

Next

Let us know your results.

Cheers

Stefan

Former Member
0 Kudos

Hi Stefan....Thanks for your reply...i did try the above codes and it is getting saved by invoice number but i want the same process to continue for the next lines on the excel sheet. Only the first invoice is getting saved in desktop. can you pls help? Thanks a mill.

Codes used as below:-

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 = "/nvf03"

session.findById("wnd[0]").sendVKey 0

Set xclapp = GetObject(, "Excel.Application")

    Set xclwbk = xclapp.Workbooks.Open("C:\Users\mohank\Desktop\Invoice Saving Attempt.xlsx")

    Set xclsht = xclwbk.Sheets("Sheet1")

    For i = 2 To xclapp.ActiveCell.SpecialCells(11).Row

        Transaction = xclsht.Cells(i, 1).Value

session.findById("wnd[0]/usr/ctxtVBRK-VBELN").text = Transaction

session.findById("wnd[0]/mbar/menu[0]/menu[11]").select

  session.findById("wnd[1]").sendVKey 37

  Set WshShell = WScript.CreateObject("WScript.Shell")

  WshShell.AppActivate "Print Preview"

  WScript.Sleep 500

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

  WScript.Sleep 250

  WshShell.SendKeys "^+s"

  WScript.Sleep 750

  WshShell.SendKeys "%n"

  WshShell.SendKeys "C:\Users\Public\Desktop\" & CStr(Transaction) & ".pdf"

  WshShell.SendKeys "%s"

  WScript.Sleep 500

Next

holger_khn
Contributor
0 Kudos

Hello.

Move this three code lines

session.findById("wnd[0]").maximize

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

session.findById("wnd[0]").sendVKey 0

to this Position

....

    For i = 2 To xclapp.ActiveCell.SpecialCells(11).Row

        Transaction = xclsht.Cells(i, 1).Value

session.findById("wnd[0]").maximize

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

session.findById("wnd[0]").sendVKey 0

session.findById("wnd[0]/usr/ctxtVBRK-VBELN").text = Transaction

....

Former Member
0 Kudos

Thanks a million Holger...It is working fine...just that there is a slight problem with 3rd line in the spreadsheet. It did save the first 2 lines of the invoices but threw the below error while processing the 3rd invoice. pls help. I really appreciate your assistance on this!!!

Code used as below:-

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

Set xclapp = GetObject(, "Excel.Application")

    Set xclwbk = xclapp.Workbooks.Open("C:\Users\mohank\Desktop\Invoice Saving Attempt.xlsx")

    Set xclsht = xclwbk.Sheets("Sheet1")

For i = 2 To xclapp.ActiveCell.SpecialCells(11).Row

        Transaction = xclsht.Cells(i, 1).Value

session.findById("wnd[0]").maximize

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

session.findById("wnd[0]").sendVKey 0

session.findById("wnd[0]/usr/ctxtVBRK-VBELN").text = Transaction

session.findById("wnd[0]/mbar/menu[0]/menu[11]").select

  session.findById("wnd[1]").sendVKey 37

  Set WshShell = WScript.CreateObject("WScript.Shell")

  WshShell.AppActivate "Print Preview"

  WScript.Sleep 500

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

  WScript.Sleep 250

  WshShell.SendKeys "^+s"

  WScript.Sleep 750

  WshShell.SendKeys "%n"

  WshShell.SendKeys "C:\Users\Public\Desktop\" & CStr(Transaction) & ".pdf"

  WshShell.SendKeys "%s"

  WScript.Sleep 500

Next

Former Member
0 Kudos

Sorry Holger....it is working fine now.

I re-tried the entire codes...it is working fine....thanks a mill:)

Former Member
0 Kudos

This message was moderated.

0 Kudos

Dear Stefan Schnell,

I readed much your intruction. I am most grateful to you.

Please give me solution for my situation.

I have code but I don't understand why it run without error. I disable in my SAP Logon options the using of the native Windows dialogs.I found that sendkey not running. So I can not SAVE file to PDF on my computer. Please help me

Hoping to hear from you!

Thank you so muchhandle.pngscripting-tracker.png

#If VBA7 Then ' Excel 2010 or later
Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal Milliseconds As LongPtr)
#Else ' Excel 2007 or earlier
Public Declare Sub Sleep Lib "kernel32" (ByVal Milliseconds As Long)
#End If


Sub test()
Dim WshShell As Object
If Not IsObject(sApplication) Then
   Set SapGuiAuto = GetObject("SAPGUI")
   Set sApplication = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
   Set Connection = sApplication.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 sApplication, "on"
End If
session.findById("wnd[0]").Maximize
session.findById("wnd[0]/tbar[0]/okcd").Text = "ztc_rp_01tbsddpnn"
session.findById("wnd[0]").SendVKey 0
session.findById("wnd[0]/usr/radP_R2").SetFocus
session.findById("wnd[0]/usr/radP_R2").Select
session.findById("wnd[0]/usr/ctxtS_TIN-LOW").Text = "8611594496"
session.findById("wnd[0]/usr/txtP_SIGN").Text = "/TB-CCT"
session.findById("wnd[0]/usr/txtP_PRNUM").Text = "1"
session.findById("wnd[0]/usr/txtP_ADRP").Text = "..."
session.findById("wnd[0]/usr/txtP_ADCON").Text = "..."
session.findById("wnd[0]/usr/txtP_PHONE").Text = "098753254"
session.findById("wnd[0]/usr/txtP_PHONE").SetFocus
session.findById("wnd[0]/usr/txtP_PHONE").CaretPosition = 12
session.findById("wnd[0]/tbar[1]/btn[8]").Press
session.findById("wnd[0]/usr/cntlZCTR_ALV_CONTAINER/shellcont/shell").SetCurrentCell -1, ""
session.findById("wnd[0]/usr/cntlZCTR_ALV_CONTAINER/shellcont/shell").SelectAll
session.findById("wnd[0]/usr/cntlZCTR_ALV_CONTAINER/shellcont/shell").PressToolbarButton "PRINT"
session.findById("wnd[1]/usr/btnBUTTON_1").Press
session.findById("wnd[0]/tbar[0]/okcd").Text = "pdf!"
session.findById("wnd[0]").SendVKey 0

Set WshShell = CreateObject("WScript.Shell")
WshShell.AppActivate "Print priview" 'Title of the window
Sleep 500
'Set the focus to the Acrobat control but check the correct ID of the control

Sleep 250
WshShell.SendKeys "+^s" 'Open save dialog
Sleep 750
WshShell.SendKeys "%n" 'Activate file name field
WshShell.SendKeys "D:\001.pdf" 'Path and file name
WshShell.SendKeys "%s" 'Save the file
Set objShell = Nothing

End Sub


0 Kudos

Dear Stefan Schnell, I try your code but it have notice: Runtime error 424 Object required at line: Set WshShell = WScript.CreateObject("WScript.Shell"). And I read on some forum that i can not use WScript. I don't understand. How can i run this code! thank so much

stefan_schnell
Active Contributor
0 Kudos

Hello mainga_hg789,
it is not clear why do you can't create an object of WScript.Shell class. In a normal case it should work without any problems. Try it on VBScript itself and send us your results.
Best regards
Stefan

Answers (2)

Answers (2)

former_member742887
Discoverer
0 Kudos

stefan.schnell, I'm relatively new to VBS and working on a similar script to print invoice automatically via VF02, but I'm unable to set the focus to the acrobat control. I receive the error 'the control could not be found by id'. How can identify the correct control?

Here my script and error:

This part is work-around to copy-paste in SAP transaction VF02.

'Create clipboard file
Set objHTML = CreateObject("htmlfile")
ClipboardText = objHTML.ParentWindow.ClipboardData.GetData("text")
InvoiceReference = InputBox("Enter invoice Number:", "Invoice Number",ClipboardText)

This will create the actual invoice in VF02

'Connect to SAP and create invoice print in VF02
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 = "/nvf02"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtVBRK-VBELN").text InvoiceReference
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/btnTC_OUTPUT").press
session.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3/ctxtDNAST-KSCHL[1,8]").text = "rd00"
session.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3/cmbNAST-NACHA[3,8]").key = "1"
session.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3/cmbNAST-NACHA[3,8]").setFocus
session.findById("wnd[0]").sendVKey 3
session.findById("wnd[0]/usr/chkNAST-DIMME").selected = true
session.findById("wnd[0]/usr/ctxtNAST-LDEST").text = "locl"
session.findById("wnd[0]/usr/txtNAST-ANZAL").text = "1"
session.findById("wnd[0]/usr/chkNAST-DIMME").setFocus
session.findById("wnd[0]").sendVKey 3
session.findById("wnd[0]").sendVKey 5
session.findById("wnd[0]/usr/cmbNAST-VSZTP").key = "4"
session.findById("wnd[0]").sendVKey 3

This is the part of the error:

'Save invoice
session.findById("wnd[0]").sendVKey 11
WScript.Sleep 500

'Set the focus to the Acrobat control but check the correct ID of the control
session.findById("wnd[0]/shellcont/shell/shellcont[1]/shell").SetFocus
WScript.Sleep 250

WshShell.SendKeys "%n" 'Activate file name field
WshShell.SendKeys "C:\Users\myusername\Desktop\001.pdf" 'Path and file name
WshShell.SendKeys "%s" 'Save the file

'Clear objHTML
Set objHTML = Nothing
Former Member
0 Kudos

This message was moderated.