cancel
Showing results for 
Search instead for 
Did you mean: 

Save SAP-generated pdf to filesystem

Former Member
0 Kudos

Hello, I am accesing sap from excel-vba and, at some place, a pdf-document is generated and opened in Adobe-Reader. Now, I need to access Adobe Reader and save the pdf-doc. Has anyone already solved this task? I'd be very happy about any help you can give me! Cheers, Stefan

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

Hello Stefan,

Can you share how you access the SAP from excel?

Former Member
0 Kudos

Hello Ahmed,

this works for me:


Public SapGuiAuto As Object

Public App As Object

Public Connection As Object

Public Session As Object

Sub YourSubName()

Set SapGuiAuto = GetObject("SAPGUI")

Set App = SapGuiAuto.GetScriptingEngine

Set Connection = App.Children(0)

Set Session = Connection.Children(0)

End Sub

Once you're there, I found the following line helpful to bring the SAP-Window to the foreground


appActivate session.findbyId("wnd[0]").text

Good luck!

Stefan

Former Member
0 Kudos

That's perfect, thanks a lot.

Now my Excel Macro can send the Tcode and the parameters to the SAP.

I need to Save as "xlsx". Can you help on this? 🙂 ..

Thanks in advance.

Former Member
0 Kudos

You can do so using the workbook's .saveAs method

https://msdn.microsoft.com/en-us/library/office/ff841185.aspx

You'll find plenty of examples googling for it ...

Former Member
0 Kudos

Sorry i  was  not clear, before starting the "save As" i need to see the generated workbook first. At the moment, I'm not able to.

Former Member
0 Kudos

If your new workbook is the last one opened/generate, this should work:

Workbooks(Workbooks.Count).SaveAs ThisWorkbook.Path & "YourFilname.xlsx"

Former Member
0 Kudos

The workbook is not generated yet, how to initiate this from the VBA?

Former Member
Former Member
0 Kudos

Thanks stefan, this is understandable. But the new workbook creation should be triggered from the SAP itself.

This is what I have at the moment

===

Public SapGuiAuto As Object

Public App As Object

Public Connection As Object

Public Session As Object

Sub CJI3_click()

Dim objExcel

Dim objSheet, intRow, i

Set SapGuiAuto = GetObject("SAPGUI")

Set App = SapGuiAuto.GetScriptingEngine

Set Connection = App.Children(0)

Set Session = Connection.Children(0)

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

Set objSheet = objExcel.ActiveWorkbook.ActiveSheet

For i = 2 To objSheet.UsedRange.Rows.Count

COL1 = Trim(CStr(objSheet.Cells(i, 1).Value)) 'Column1

col2 = Trim(CStr(objSheet.Cells(i, 2).Value)) 'Column2

col3 = Trim(CStr(objSheet.Cells(i, 3).Value)) 'Column3

col4 = Trim(CStr(objSheet.Cells(i, 4).Value)) 'Column4

col5 = Trim(CStr(objSheet.Cells(i, 5).Value)) 'Column5

Rem ADDED BY EXCEL *************************************

AppActivate Session.findById("wnd[0]").Text 'this to give focus to SAP and bring it to foreground

Session.findById("wnd[0]").resizeWorkingPane 226, 39, False

Session.findById("wnd[0]/tbar[0]/okcd").Text = "/ncji3"

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

If Session.ActiveWindow.Name = "wnd[1]" Then

  Session.findById("wnd[1]/usr/ctxtTCNT-PROF_DB").Text = "z1000"

  Session.findById("wnd[1]").sendVKey 0

End If

Session.findById("wnd[0]/usr/ctxtCN_PROJN-LOW").Text = COL1

Session.findById("wnd[0]/usr/ctxtR_BUDAT-LOW").Text = col2

Session.findById("wnd[0]/usr/ctxtR_BUDAT-HIGH").Text = col3

Session.findById("wnd[0]/usr/ctxtP_DISVAR").Text = col4

Session.findById("wnd[0]/usr/btnBUT1").press

Session.findById("wnd[1]/usr/txtKAEP_SETT-MAXSEL").Text = col5

Session.findById("wnd[1]/tbar[0]/btn[0]").press

Session.findById("wnd[0]/tbar[1]/btn[8]").press

Session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").contextMenu

Session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").selectContextMenuItem "&XXL"

Session.findById("wnd[1]/usr/cmbG_LISTBOX").Key = "10"

Session.findById("wnd[1]/tbar[0]/btn[0]").press

'The save As window comes up here and ask for the file name

Workbooks.Application.DisplayAlerts = False

Workbooks.Item(Workbooks.Count).SaveAs ("C:\tmp\" & COL1 & ".xls")

Workbooks.Item("export.xlsx").Close

Rem FINALIZATION CONTROL CHECK ************************

aux = COL1 & " " & col2 & " " & col3 & " " & col4 & " " & col5

CreateObject("WScript.Shell").Run ("cmd /c @echo %date% %time% " & aux & " >> C:\SCRIPT\PlOrCreationLog.txt")

Next

MsgBox "Process Completed"

Rem FINALIZATION CONTROL CHECK ************************

End Sub

===

Former Member
0 Kudos

The save-dialogue is just another window you have to adress: If main window is wnd[0], the dialogue-window should be wnd[1].

Why don't you use the script recorder to see the correct paths and commands? How did you find the rest of your code?

Former Member
0 Kudos

Unfortunately, the Save as window is not a GUI window. As such, the script recording tool is not able to record the action there.

holger_khn
Contributor
0 Kudos

This depence on SAP_BASIS Release used in Company. We are on 740 and there we can use GUI-modal SaveAs dialogues which are recordable.

Former Member
0 Kudos

Maybe this could work to find and set focus to your save-as dialogue window


AppActivate("Save As")

Once found, you can use sendkey-commands to enter your path & filename and save... At least, that's how I managed to save pdfs.

Former Member
0 Kudos

I'm running this from a vba in excel

When you run the Cij3, the following will happen

1- The report is generated on the SAP - no excel workbook is opened yet-

2- You will right click on the generated report and select "Spreadsheet". or Ctr+Shift+F7

3- A window to select the spreadsheet type will pop-up

4- once you click on the green button, you will have the "save as" window as following

For every line in the VBA code you will get the control back to VBA except step (4) above. Once you click on the green button on the "select spreadsheet" window - presented by

Session.findById("wnd[1]/tbar[0]/btn[0]").press

The focus/control will stay in the SAP till you enter a file name. This is why any code in the VBA is not going to work 🙂

Former Member
0 Kudos

Once you click on the green button on the "select spreadsheet" window - presented by

Session.findById("wnd[1]/tbar[0]/btn[0]").press

The focus/control will stay in the SAP till you enter a file name.

If AppActivate ("Save As") doesn't work at this point, there are ways to loop through all windows and find the right one, try searching for this...

Former Member
0 Kudos

Hello Ahmed,

I did some research, you should be able to solve this using Windows API functions:

FindWindow function (Windows)

You can pass the Windows Name ("Save As") as an argument and the function will return the window handle

SetForegroundWindow function (Windows)

This Function takes the window handle as argument and will set it as foreground window

Then you should be able to proceed using sendkey commands.

Former Member
0 Kudos

Hello Ahmed,

here's how it's going to work:

Former Member
0 Kudos

I read that the save-as issue is resolved in GUI 740, but I'm not able to do it 😞

Former Member
0 Kudos

I'm on 740now, what do I need to do?