Используйте AppActivate, чтобы сохранить как
Я работаю над макросом, который будет получать PDF из SAP в соответствии со списком счетов, который я предоставлю в столбце "А". Эти счета будут сохранены в указанном пути "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''''
Возникла проблема с окном "Сохранить как", которое не отображается. Макрос остановился в "Предварительном просмотре PDF", и я хотел бы перейти к следующему шагу "Сохранить копию", после чего появится окно "Сохранить как", но я не могу предоставить правильный код для этого
2 ответа
Решение
Я почти уверен, что вы найдете решение вашей проблемы здесь:
https://answers.sap.com/questions/12057563/scripts-to-save-a-pdf-copy-of-a-doc-from-sap-scree.html
С уважением, ScriptMan
Я думаю, что это должно быть использовано так:
WshShell.AppActivate"Save PDF File As"
WshShell.SendKeys FileName