SAP 另存为停止 VBA 脚本执行

SAP Save As stops VBA script execution

我正在编写一个 VB 代码,用于从 SAP 保存 PDF 文件。我已经到了SAP询问我想保存我的pdf文件的地方(打开windows explorer "save as" window)。此时,VB一个代码停止,我需要手动输入要保存的文件的名称。然后,vba 继续 运行...

我需要帮助来找到自动执行此步骤的方法。

我正在考虑的一个可能的解决方案(但不知道如何实际去做)是告诉 vba 到 运行 一个 VB 脚本结束于另存为 window。然后我会发送一个 "application.sendkeys(" ") 来输入另存为路径。

请告知是否可行。如果是,下一步是我将不得不动态修改 vb 脚本文件的特定行(我需要遍历列表并每次更改一些值)

谢谢

所以,这是一个相当大的挑战....这是我处理 "Save as" window 的解决方案。如果您只想单击 "Save" 按钮,它会更简单。我的解决方案比较复杂,因为我指定了文件需要保存的位置。为此,您需要找到正确的组合框,这需要大量迭代。

WinAPI 必要声明:

    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
(ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" _
(ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias _
 "SendMessageW" (ByVal hWnd As Long, ByVal wMsg As Long, _
 ByVal wParam As Long, lParam As Any) As Long

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long


Public Declare Function SendNotifyMessage Lib "user32" Alias "SendMessageA" ( _
    ByVal hWnd As Long, _
    ByVal Msg As Integer, _
    ByVal ByValByValwParam As Integer, _
    ByVal lParam As String) As Integer

实际VBA代码:

Sub SaveAsWindow()
Dim Winhwnd As Long
Dim prev As Long
Dim abc As Long
Dim strText As String
Dim rty As Variant
Dim Parent As Long
Dim child As Long
Winhwnd = FindWindow(vbNullString, "Save As")

For i = 1 To 20
  strText = String$(100, Chr$(0))
  abc = GetClassName(Winhwnd, strText, 100)  
  If Left$(strText, 12) = "DirectUIHWND" Then GoTo here1
  Winhwnd = FindWindowEx(Winhwnd, 0&, vbNullString, vbNullString)
Next i

here1:

Parent = Winhwnd
child = FindWindowEx(Parent, 0&, vbNullString, vbNullString)

GoTo skip 'avoid this part for the 1st run

here2:
'fix child3 and child2
If child2 = 0 Then
    rty = "0&"
    Else
    rty = 0
End If
If child3 = 555 Then
  rty = "0&"
  child3 = ""
End If


skip:

For i = 1 To 20
    child = FindWindowEx(Parent, child, vbNullString, vbNullString)

    For x = 1 To 20
        If child3 = "" Then rty = 0
        child2 = FindWindowEx(child, rty, vbNullString, vbNullString)
        abc = GetClassName(child2, strText, 100)

            If Left$(strText, 8) = "ComboBox" Then
                child3 = FindWindowEx(child2, 0&, vbNullString, vbNullString)
                If child3 = 0 Then
                child3 = 555
                GoTo here2
                Else
                GoTo here3
            End If
        End If
    Next x
Next i

here3:
'this is te filepath. will be pasted into combobox. to adapt to your needs.
SendNotifyMessage child3, &HC, 0&, "C:\Users\username\abc.pdf"


'Get again the Save button
Winhwnd = FindWindow(vbNullString, "Save As")
buttn = FindWindowEx(Winhwnd, 0, "Button", "&Save")

'click on the save button
SendMessage buttn, &HF5&, 0, 0

End Sub

第 2 VBA 代码:对于 SAP,由于使用 ComboboxEx32 而不是 Combobox,事实证明它更简单。

Sub test()
Dim Winhwnd As Long
Dim strText As String
Winhwnd = FindWindow(vbNullString, "Save As")

combo = FindWindowEx(Winhwnd, 0, vbNullString, vbNullString)

For i = 1 To 20
combo = FindWindowEx(Winhwnd, combo, vbNullString, vbNullString)
strText = String$(100, Chr$(0))
abc = GetClassName(combo, strText, 100)

If Left$(strText, 12) = "ComboBoxEx32" Then GoTo here

Next i
here:

SendNotifyMessage combo, &HC, 0&, "C:\Users\username\abc.pdf"

buttn = FindWindowEx(Winhwnd, 0, "Button", "&Open")
SendMessage buttn, &HF5&, 0, 0

End Sub

最重要的是,这不是最完美的代码,但我在网上找不到任何其他代码。我希望这会对遇到同样问题的任何人有所帮助。