cancel
Showing results for 
Search instead for 
Did you mean: 

Creating pdf and attaching to email

0 Kudos

Hello,

I am trying to write a script to help manual work and am stuck at below point:

When click save in SAP I get pdf document generated (nhremote). What I would like to do is to somehow automatically save it on my drive and attach it in the new email that is generated after each save.

Script end:

'session.findById("wnd[1]/usr/btnSPOP-VAROPTION1").press - SAVING the changes and pdf is generated at this point. takes some time to popup.

'Set MyApp = CreateObject("Outlook.Application") 

'Set MyItem = MyApp.CreateItem(0) 'olMailItem 

'With MyItem 

  '.To = "a@b.c"

  '.Subject = "Subject"

  '.ReadReceiptRequested = False

  '.HTMLBody = RDD

'End With 

'MyItem.Display

is there any way I can put action to save and attach saved file to the email? Also please keep in mind that the script is looped and it does this for many documents. Since NHREMOTE to generate pdf usually takes sometime and is a popup window I'm afraid it can be very complicated and mess up easily.

Accepted Solutions (1)

Accepted Solutions (1)

stefan_schnell
Active Contributor
0 Kudos

Hello Gediminas,

try this:

'-Begin-----------------------------------------------------------------

  '-Directives----------------------------------------------------------
    Option Explicit

  '-Variables-----------------------------------------------------------
    Dim application, SapGuiAuto, connection, session, RepName
    Dim WScr, MyApp, MyItem, FileName

  '-Main----------------------------------------------------------------
    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(1)
    End If

    If IsObject(WScript) Then
      WScript.ConnectObject session, "on"
      WScript.ConnectObject application, "on"
    End If

    Set WScr = CreateObject("WScript.Shell")
    If IsObject(WScr) Then
      RepName = "PROGDELE"
      FileName = "C:\Schnell\Dummy\" & RepName & ".pdf"

      session.findById("wnd[0]/tbar[0]/okcd").text = "/nSE38"
      session.findById("wnd[0]/tbar[0]/btn[0]").press
      session.findById("wnd[0]/usr/ctxtRS38M-PROGRAMM").text = RepName
      session.findById("wnd[0]/usr/btnSHOP").press

      WScript.Sleep 500
      WScr.AppActivate "ABAP Editor: Report " & RepName & " anzeigen"
      WScript.Sleep 2000
      session.findById("wnd[0]/usr/cntlEDITOR/shellcont/shell").SetFocus()
      WScr.SendKeys "+{F5}" 'Customize Shift+F5 to File.ExportPDF
      WScript.Sleep 250
      wscr.SendKeys FileName
      WScript.Sleep 250
      wscr.SendKeys "{ENTER}"
      WScript.Sleep 1000

      Set MyApp = CreateObject("Outlook.Application")
      If IsObject(MyApp) Then
        Set MyItem = MyApp.CreateItem(0)
        With MyItem
          .To = "hugo.bambi@fantasie.org"
          .Subject = "Subject"
          .ReadReceiptRequested = False
          .Body = "Here comes the file"
          .Attachments.Add FileName
        End With
        MyItem.Display
        'MyItem.Send
        Set MyItem = Nothing

        Set MyApp = Nothing
      End If

      Set WScr = Nothing
    End If

'-End-------------------------------------------------------------------

This script starts the TAC SE38 and loads the Report PROGDELE. It exports the report as PDF and sends the PDF file via e-mail to hugo.bambi.

All you have to do, to try this script, is to customize your SE38 view, that the shortcut Shift+F5 ist set to File.ExportPDF.

You see, it is possible to save a PDF from SAP and to send it as e-mail attachment . Also you see it is possible to handle SAP dialog windows. And it should be not a great problem to do this steps in loop for more than one document.

Let us know your result.

Cheers

Stefan

0 Kudos

Hi Stefan,

thanks for sharing script. I have few issues unfortunately. First is that I do not have rights to this transaction, but will see if i can get. Also, would there be a way to create a one time setting for pdf files to be generated to disk directly while applying name of document number?

Second, in my business unit we use NetWeaver that has cockpit which works different from old SAP FrontEnd and does not seem to be able to call or close any transactions which can make this solution a bit difficult to use since I will have to juggle between transactions each time if I understood right.

Initial idea was not to exit transaction(va02) and do many changes to many orders while generating printouts.

Another workaround I may have found is to use pdf995 as default printer, which actually has a lot of settings such as autosave and also can run a .vbs script after saving.

Perfect solution would be to have output generate and save document without any need of extra steps.

stefan_schnell
Active Contributor
0 Kudos

Hello Gediminas,

the scripts shows how to handle a PDF file creation with a dialog via SAP GUI Scripting, the TAC SE38 shows only an example of the procedure.

You must change your script to your special environment and requirement.

As far I understand you right, is your Cockpit an SAP application and that should be controlled via SAP GUI Scripting. So I think you have to do similar steps as in the script above.

Cheers

Stefan


0 Kudos

Hi Stefan,

I don't have experience with SAP GUI scripting and as mentioned I do not have access to transaction for the moment so it is not very clear how it should work in my case.

To explain bit better my situation. Once order changes are done, before saving I go to output via menu and create a spool request to NHREMOTE which then SAP sends to default printer after saving. All this done in (va02) transaction and is no problem. The issue becomes when SAPLPD launches and then Save As button appears. At this point I have no way of aumating saving step nor skipping it.

BR,

Gedis

stefan_schnell
Active Contributor
0 Kudos

Hello Gedis,

as far I understand you correctly is this your point of origin:

The field Filename is activated.

Try this:

  Set WScr = CreateObject("WScript.Shell")

  If IsObject(WScr) Then

    FileName = "C:\Schnell\Dummy\Filename.pdf"

    WScript.Sleep 500

    WScr.SendKeys FileName

    WScript.Sleep 250

    WScr.SendKeys "{ENTER}"

    WScript.Sleep 1000

    Set WScr=Nothing

  End If

You know the filename and now you can send it via e-Mail.

Cheers

Stefan

0 Kudos

thanks alot Stefan! This is exactly what I needed. Only adjustment i had to do was to put sleep timer and let pdf save as window to pop up or otherwise it would put filename into transaction window.

It's somewhat feels mechanical with waiting time, as sometimes system works slow and prinout pops up much later. I wonder if there's any command to instruct waiting until pop up appears.

stefan_schnell
Active Contributor
0 Kudos

Hello Gedis,

try this, to find a special window or Dialog:


Set WScr = CreateObject("WScript.Shell")

Do
  WScript.Sleep 50
Loop Until WScr.AppActivate("Unbenannt - Editor")

Set WScr = Nothing

MsgBox "End"

AppActivate needs the title of the window. This is a little bit like FindWindow..

Cheers

Stefan

0 Kudos

Hi Stefan,

Bless you. You really helped me out here. I got only some details left to be polished, but rough layout is done. Thanks a bunch!

BR,

Gedis

stefan_schnell
Active Contributor
0 Kudos

Hello Gedis,

you are welcome.

Cheers

Stefan

holger_khn
Contributor
0 Kudos

Hello.

If you have no Access for Transaction SE38 you may check if you have Access for Transaction START_REPORT

It´s quite the same to start ABAP Report without Access for SE38.

Best regards,

Holger

0 Kudos

Hi Stefan,

I was hoping you could help me with small issue. Sometimes save as popup does not appear on top and save location is typed into SAP field instead. Is there anyway to focus view on the save as window?

Thanks!

stefan_schnell
Active Contributor
0 Kudos

Hello Gedis,

I am afraid I can not really help you. Under normal circumstances AppActivate should bring the window in the foreground and take the focus to it - look here. I don't know, why it doesn't work sometimes in your case. Maybe, another window seems to push in between. Does anyone else have a suggestion?

Cheers

Stefan

Answers (0)