on 04-27-2015 8:41 PM
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
Hello Stefan,
Can you share how you access the SAP from excel?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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 ...
Try to understand and adjust what you learn to your needs...
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
===
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 🙂
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...
Hello Ahmed,
I did some research, you should be able to solve this using Windows API functions:
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.