【问题标题】:SAP Save As stops VBA script executionSAP 另存为停止 VBA 脚本执行
【发布时间】:2017-04-08 23:10:33
【问题描述】:

我正在编写一个 VBA 代码,它将保存来自 SAP 的 PDF 文件。我已经到了 SAP 询问我要将我的 pdf 文件保存在哪里的地方(打开 Windows 资源管理器“另存为”窗口)。此时,VBA 代码停止,我需要手动输入我想要的文件的名称保存。然后,vba继续运行……

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

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

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

谢谢

【问题讨论】:

  • 调用Excel的另存为对话框比较容易。此处social.msdn.microsoft.com/Forums/en-US/… 讨论了此类代码。除此之外的任何事情都需要查看您现有的代码以更好地理解您的问题。
  • 嗨 Variatus,它的 SAP 调用了对话框,而不是我。当它执行此操作时,我无法运行任何代码,因为 VBA 希望我在恢复其余代码之前关闭它。谢谢。
  • 让我直说。 SAP 是否调用 Excel 为其提供一些数据并将 Excel 工作表保存为 PDF?
  • 您可以使用 AHK。
  • @Variatus,SAP 正在调用“另存为”窗口来保存 pdf 文件。

标签: excel vbscript sap vba


【解决方案1】:

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

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

第二个 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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-27
    • 1970-01-01
    • 2011-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-01
    相关资源
    最近更新 更多