嗨, 我正在建立一个宏以自动从SAP下载发票的PDF。我已经弄清楚了,但是我总是卡在WScript-对象必需的错误上。 有人能帮我吗? 这是代码:
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
我总是卡在第一个WScript.Sleep上。谁知道为什么? 当我运行宏时,它显示“运行时错误424:需要对象”,而当我在SAP上运行脚本时,出现错误“对象需要:WScript-”
谢谢