cancel
Showing results for 
Search instead for 
Did you mean: 

SAP vbs script to save multiple Print Preview PDF documents?

Former Member
0 Kudos

Hello,

First time post/question. I'm still very new to vbs, but having an issue creating what I hoped would be an 'easy' one.

Before running the script, I use a select all, then a Print Preview (of sorts), which generates an Adobe PDF inside SAP. I simply want the script to hit ctrl + s, hit enter on the windows Save As window to save it to the default location, then click Next and loop until the Next button cannot be found. Note that each time the Save As window comes up, it is a custom file name already--so I don't need to worry about unique file names.

I'm starting light and using this on a selection of 4 documents. The script runs and is able to save off the first pdf file, but then appears to go the last document and stops (cannot tell if it's running too quickly or what).

I have done a bunch of searching and tried numerous alternate approaches without luck. Commands like Wscript.sleep never seem to work whether or not I do them inside a single vbs or put a command in a 2nd vbs file to reference as they give me 'Object Not Found' errors.

Please, if you have something for me to reference, please link me to it instead of asking me to search. Chances are, I've probably read it already haha.

Any ideas?

Again, I'm still new so if you have pointers, please be as specific as you can. Thanks! (I apologieze, but 'Copy Code' was working very poorly for me.)

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
Set wshell = CreateObject("WScript.Shell")
Do Until session.findById("wnd[0]/sbar/pane[0]").text = "Function code cannot be selected"
session.findById("wnd[0]/usr/cntlHTML_IFBA_PREVIEW/shellcont/shell").SetFocus
wshell.SendKeys "^s"
wshell.SendKeys "{ENTER}"
session.findById("wnd[0]/tbar[1]/btn[39]").press
Loop
MsgBox "Complete"

Accepted Solutions (0)

Answers (1)

Answers (1)

stefan_schnell
Active Contributor
0 Kudos

Hello Eric,

welcome in this forum and at SAP GUI Scripting.

Please take a look at this archived thread, hope this answer your question, otherwise let us know.

Cheers
Stefan

Former Member
0 Kudos

Hi Stefan,

I actually used your Scripting Tracker to find the PDF screen and the line is already in the script above. It worked great!! My issue was that the script was running too quickly and I could not slow it down using Wscript.sleep commands because I was getting the "Object Required: 'Wscript' -" error.

I had no idea that the script would run differently if executed from a command line using wscript.exe "C:\Example.vbs" instead of using SAP Recording and Playback. I spent so much time trying to work alternative approaches too, figuring that the top section (the wscript.connectobject lines) was turning on wscript.exe in order to run it properly. When I ran it through the command line, it recognized Wscript just fine. (Ref. your blog post: https://blogs.sap.com/2014/01/05/how-an-sap-gui-script-is-executed/ )

My current code is below but it's still not ideal. I ran ~1200 PDFs and it 'randomly' missed 9 of them--not sure why. It also took wayyyy too long since I basically put a ~2 second gap in between every single step since it seemed it was running too quickly. I'm thinking of trying a Do Loop using AppActivate to find the 'Save As' window (now that using a command line works better), but not very familiar with AppActivate yet. I'm on SAP 730, so I have no option to turn off the Windows Save As window and use SAPs. Besides AppActivate, I was considering counting the number of files in the save folder having a Do Loop until the # of files in the folder increased by 1, but didn't get that working (at least not yet).

Any ideas for improvement?

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
Set wshell = CreateObject("WScript.Shell")
Do
  session.findById("wnd[0]/usr/cntlHTML_IFBA_PREVIEW/shellcont/shell").SetFocus
  WScript.Sleep 2000  
  wshell.SendKeys "^s"
  WScript.Sleep 2400
  wshell.SendKeys "{ENTER}"
  WScript.Sleep 2400
  session.findById("wnd[0]/tbar[1]/btn[39]").press
  WScript.Sleep 2200
Loop Until session.findById("wnd[0]/sbar/pane[0]").text = "Function code cannot be selected"
MsgBox "Complete"
Set wshell = Nothing


Thanks!
-Eric

stefan_schnell
Active Contributor
0 Kudos

Hello Eric,

thanks for your reply. I think you reach with your script an excellent result. It is possible that the size of the PDF files are in some cases to big and the loading time of the file exceeds the sleeping time or the network traffic is to high so that the transfer speed is to slow.

Here an example how to check if the "Save as" dialog exists:

While Not wshell.AppActivate("Save as")
  WScript.Sleep 250
Wend<br>

Is it possible for you to use another scripting language? AutoIt offers a lot of possibilities to control the UI, take a look at it.

Cheers
Stefa