2017-04-08 80 views
1

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

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

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

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

谢谢

+0

调用Excel的SaveAs对话框相对比较简单。 https://social.msdn.microsoft.com/Forums/en-US/69e81793-94d2-49a1-ab82-a3acdb4c81d4/saveas-dialog-box?forum=isvvba有关于此类代码的讨论。除此之外,任何事情都需要查看您现有的代码以更好地理解您的问题。 – Variatus

+0

嗨Variatus,其称为对话的SAP,而不是我。当它这样做时,我无法运行任何代码,因为VBA希望在恢复代码的其余部分之前关闭它。谢谢。 – OGERWOLF

+0

让我直说吧。 SAP调用Excel来给它一些数据并将Excel表保存为PDF? – Variatus

回答

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被用来代替组合框。底线,这不是最完美的代码,但我在网上找不到其他东西。我希望这会给有相同问题的人带来好处。