使用 AppActivate 另存为
Use AppActivate to Save As
我正在开发一个宏,它将根据我将在 "A" 列中提供的发票清单从 SAP 获取 PDF。此发票将保存在给定路径 "D6" 中。
Public Declare PtrSafe Function GetActiveWindow Lib "user32" () As Long
Sub save_invoice()
Dim sInvName As String
Dim sPath As String
Dim WshShell
Set WshShell = CreateObject("WScript.Shell")
lr = Range("A" & Rows.Count).End(xlUp).Row 'count all given invoice numbers
sPath = Range("D6") 'in this cell we provide path in which all PDF's should be saved
If Not IsObject(App) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set App = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = App.Children(0)
End If
If Not IsObject(session) Then
Set session = Connection.Children(0)
End If
If IsObject(WScript) Then
WScript.ConnectObject session, "on"
WScript.ConnectObject App, "on"
End If
session.findById("wnd[0]/tbar[0]/okcd").Text = "/nvf03"
session.findById("wnd[0]").sendVKey 0
For i = 1 To lr
session.findById("wnd[0]/usr/ctxtVBRK-VBELN").Text = Range("A" & i)
sInvName = Range("A" & i)
session.findById("wnd[0]/mbar/menu[0]/menu[11]").Select
session.findById("wnd[1]/usr/tblSAPLVMSGTABCONTROL").getAbsoluteRow(0).Selected = True
session.findById("wnd[1]/tbar[0]/btn[37]").press
session.findById("wnd[0]/tbar[0]/okcd").Text = "pdf!"
session.findById("wnd[0]").sendVKey 0
'open "PDF Preview" window
WshShell.AppActivate "PDF Preview"
Do
Loop Until WshShell.AppActivate("PDF Preview") = True
WshShell.SendKeys "{ENTER}"
Application.Wait (Now + TimeValue("0:00:01"))
WshShell.SendKeys "{TAB}"
Application.Wait (Now + TimeValue("0:00:01"))
WshShell.SendKeys "{TAB}"
Application.Wait (Now + TimeValue("0:00:01"))
WshShell.SendKeys "{TAB}"
Application.Wait (Now + TimeValue("0:00:01"))
WshShell.SendKeys "^+s" 'Open save dialog
WshShell.SendKeys "{ENTER}"
'>>>>here should be open "Save As" window
Do
WshShell.SendKeys "{TAB}"
Application.Wait (Now + TimeValue("0:00:01"))
WshShell.SendKeys "{TAB}"
Application.Wait (Now + TimeValue("0:00:01"))
WshShell.SendKeys "^+s"
Loop Until WshShell.AppActivate("Save As") = True
Application.Wait (Now + TimeValue("0:00:01"))
WshShell.SendKeys "%n" 'Save the file
Application.Wait (Now + TimeValue("0:00:01"))
WshShell.SendKeys sPath & sInvName & ".pdf" 'Path and file name
Application.Wait (Now + TimeValue("0:00:01"))
WshShell.SendKeys "%+s" 'Save the file
session.findById("wnd[1]").Close
session.findById("wnd[0]/tbar[0]/btn[3]").press
session.findById("wnd[1]").Close
Next i
End Sub''''
"Save as" window 有一些问题没有出现。宏在 "PDF Preview" 停止,我想转到 "Save a copy" 的下一步,然后会出现 "Save as" window,但我无法为此提供正确的代码
我几乎可以肯定您会在这里找到问题的解决方案:
https://answers.sap.com/questions/12057563/scripts-to-save-a-pdf-copy-of-a-doc-from-sap-scree.html
问候,ScriptMan
我正在开发一个宏,它将根据我将在 "A" 列中提供的发票清单从 SAP 获取 PDF。此发票将保存在给定路径 "D6" 中。
Public Declare PtrSafe Function GetActiveWindow Lib "user32" () As Long
Sub save_invoice()
Dim sInvName As String
Dim sPath As String
Dim WshShell
Set WshShell = CreateObject("WScript.Shell")
lr = Range("A" & Rows.Count).End(xlUp).Row 'count all given invoice numbers
sPath = Range("D6") 'in this cell we provide path in which all PDF's should be saved
If Not IsObject(App) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set App = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = App.Children(0)
End If
If Not IsObject(session) Then
Set session = Connection.Children(0)
End If
If IsObject(WScript) Then
WScript.ConnectObject session, "on"
WScript.ConnectObject App, "on"
End If
session.findById("wnd[0]/tbar[0]/okcd").Text = "/nvf03"
session.findById("wnd[0]").sendVKey 0
For i = 1 To lr
session.findById("wnd[0]/usr/ctxtVBRK-VBELN").Text = Range("A" & i)
sInvName = Range("A" & i)
session.findById("wnd[0]/mbar/menu[0]/menu[11]").Select
session.findById("wnd[1]/usr/tblSAPLVMSGTABCONTROL").getAbsoluteRow(0).Selected = True
session.findById("wnd[1]/tbar[0]/btn[37]").press
session.findById("wnd[0]/tbar[0]/okcd").Text = "pdf!"
session.findById("wnd[0]").sendVKey 0
'open "PDF Preview" window
WshShell.AppActivate "PDF Preview"
Do
Loop Until WshShell.AppActivate("PDF Preview") = True
WshShell.SendKeys "{ENTER}"
Application.Wait (Now + TimeValue("0:00:01"))
WshShell.SendKeys "{TAB}"
Application.Wait (Now + TimeValue("0:00:01"))
WshShell.SendKeys "{TAB}"
Application.Wait (Now + TimeValue("0:00:01"))
WshShell.SendKeys "{TAB}"
Application.Wait (Now + TimeValue("0:00:01"))
WshShell.SendKeys "^+s" 'Open save dialog
WshShell.SendKeys "{ENTER}"
'>>>>here should be open "Save As" window
Do
WshShell.SendKeys "{TAB}"
Application.Wait (Now + TimeValue("0:00:01"))
WshShell.SendKeys "{TAB}"
Application.Wait (Now + TimeValue("0:00:01"))
WshShell.SendKeys "^+s"
Loop Until WshShell.AppActivate("Save As") = True
Application.Wait (Now + TimeValue("0:00:01"))
WshShell.SendKeys "%n" 'Save the file
Application.Wait (Now + TimeValue("0:00:01"))
WshShell.SendKeys sPath & sInvName & ".pdf" 'Path and file name
Application.Wait (Now + TimeValue("0:00:01"))
WshShell.SendKeys "%+s" 'Save the file
session.findById("wnd[1]").Close
session.findById("wnd[0]/tbar[0]/btn[3]").press
session.findById("wnd[1]").Close
Next i
End Sub''''
"Save as" window 有一些问题没有出现。宏在 "PDF Preview" 停止,我想转到 "Save a copy" 的下一步,然后会出现 "Save as" window,但我无法为此提供正确的代码
我几乎可以肯定您会在这里找到问题的解决方案:
https://answers.sap.com/questions/12057563/scripts-to-save-a-pdf-copy-of-a-doc-from-sap-scree.html
问候,ScriptMan