我正在编写一个VBA代码,用于保存SAP的PDF文件。我已到达SAP问我要保存pdf文件的位置(打开Windows资源管理器“另存为”窗口)。此时,VBA代码停止,我需要手动输入我想要的文件名保存。然后,vba继续运行......
我需要帮助才能找到自动执行此步骤的方法。
我正在考虑的一个可能的解决方案(但不知道如何实际执行)是告诉vba运行一个以save作为窗口结束的VB脚本。然后我会发送一个“application.sendkeys(”“)来输入save as path。
请告知是否可行。如果是,下一步是我将动态修改vb脚本文件的特定行(我需要遍历列表并每次更改一些值)
谢谢
答案 0 :(得分: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
最重要的是,这不是最完美的代码,但我在网上找不到任何其他内容。我希望这对有同样问题的人有所帮助。