Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

SAP Script Run from Excel Macro - VF03

Former Member
0 Likes
1,383

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

2 REPLIES 2
Read only

Sandra_Rossi
Active Contributor
0 Likes
921

You should ask on a WScript forum... (or better, search, because such a question has been probably asked many times...)

Read only

Former Member
0 Likes
921

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