2019 Mar 24 1:16 PM
Hi,
I'm buidling a Macro to automate the download the PDF of an invoice from SAP. I've got it mostly figured out, but I always get stuck on WScript - Object required error.
Can someone help me?
Here's the code:
Private Sub CommandButton2_Click()
If Not IsObject(SAPguiAPP) Then
Set SAPguiAuto = GetObject("SAPGUI")
Set SAPguiAPP = SAPguiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = SAPguiAPP.Children(0)
End If
If Not IsObject(SAP_Session) Then
Set SAP_Session = Connection.Children(0)
End If
If IsObject(WScript) Then
WScript.ConnectObject SAP_Session, "on"
WScript.ConnectObject SAPguiAPP, "on"
End If
Set xclapp = GetObject(, "Excel.Application")
Set xclwbk = xclapp.Workbooks.Open("C:\Users\PS1043272\Desktop\CNPDF.xlsm")
Set xclsht = xclwbk.Sheets("Sheet1")
For i = 2 To xclapp.ActiveCell.SpecialCells(11).Row
INVCN = xclsht.Cells(2, 1).Value
SAP_Session.FindById("wnd[0]").maximize
SAP_Session.FindById("wnd[0]/tbar[0]/okcd").Text = "/nvf03"
SAP_Session.FindById("wnd[0]").sendVKey 0
SAP_Session.FindById("wnd[0]/usr/ctxtVBRK-VBELN").Text = INVCN
SAP_Session.FindById("wnd[0]/mbar/menu[0]/menu[11]").Select
SAP_Session.FindById("wnd[1]").sendVKey 37
Set WshShell = CreateObject("WScript.Shell")
WshShell.AppActivate "Print Preview"
SAP_Session.FindById("wnd[0]/tbar[0]/okcd").Text = "pdf!"
SAP_Session.FindById("wnd[0]").sendVKey 0
WScript.Sleep 500
SAP_Session.FindById("wnd[1]/usr/cntlHTML/shellcont/shell").SetFocus
WScript.Sleep 250
WshShell.SendKeys "^+s"
WScript.Sleep 750
WshShell.SendKeys "%n"
WshShell.SendKeys "C:\Users\PS1043272\Desktop\" & CStr(INVCN) & "teste.pdf"
WshShell.SendKeys "%s"
WScript.Sleep 500
Next
End Sub
I awlays get stuck on the first WScript.Sleep. anyone knows why?
when i run the macro it says "run-time Error 424: Object Required" and when i run the script on SAP I get the error "Object Required: WScript -"
Thanks
Hi,
I'm buidling a Macro to automate the download the PDF of an invoice from SAP. I've got it mostly figured out, but I always get stuck on WScript - Object required error.
Can someone help me?
Here's the code:
Private Sub CommandButton2_Click()
If Not IsObject(SAPguiAPP) Then
Set SAPguiAuto = GetObject("SAPGUI")
Set SAPguiAPP = SAPguiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = SAPguiAPP.Children(0)
End If
If Not IsObject(SAP_Session) Then
Set SAP_Session = Connection.Children(0)
End If
If IsObject(WScript) Then
WScript.ConnectObject SAP_Session, "on"
WScript.ConnectObject SAPguiAPP, "on"
End If
Set xclapp = GetObject(, "Excel.Application")
Set xclwbk = xclapp.Workbooks.Open("C:\Users\PS1043272\Desktop\CNPDF.xlsm")
Set xclsht = xclwbk.Sheets("Sheet1")
For i = 2 To xclapp.ActiveCell.SpecialCells(11).Row
INVCN = xclsht.Cells(2, 1).Value
SAP_Session.FindById("wnd[0]").maximize
SAP_Session.FindById("wnd[0]/tbar[0]/okcd").Text = "/nvf03"
SAP_Session.FindById("wnd[0]").sendVKey 0
SAP_Session.FindById("wnd[0]/usr/ctxtVBRK-VBELN").Text = INVCN
SAP_Session.FindById("wnd[0]/mbar/menu[0]/menu[11]").Select
SAP_Session.FindById("wnd[1]").sendVKey 37
Set WshShell = CreateObject("WScript.Shell")
WshShell.AppActivate "Print Preview"
SAP_Session.FindById("wnd[0]/tbar[0]/okcd").Text = "pdf!"
SAP_Session.FindById("wnd[0]").sendVKey 0
WScript.Sleep 500
SAP_Session.FindById("wnd[1]/usr/cntlHTML/shellcont/shell").SetFocus
WScript.Sleep 250
WshShell.SendKeys "^+s"
WScript.Sleep 750
WshShell.SendKeys "%n"
WshShell.SendKeys "C:\Users\PS1043272\Desktop\" & CStr(INVCN) & "teste.pdf"
WshShell.SendKeys "%s"
WScript.Sleep 500
Next
End Sub
I awlays get stuck on the first WScript.Sleep. anyone knows why?
when i run the macro it says "run-time Error 424: Object Required" and when i run the script on SAP I get the error "Object Required: WScript -"
Thanks
2019 Mar 24 1:42 PM
You should ask on a WScript forum... (or better, search, because such a question has been probably asked many times...)
2019 Apr 02 2:10 PM
Hello Pedro,
Did you manage to resolve your problem? Is there any option to automize via macro the VF03 transation? Kind regards and many thanks,
Natalia
| User | Count |
|---|---|
| 3 | |
| 2 | |
| 2 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |