SAP 另存为停止 VBA 脚本执行 [英] SAP Save As stops VBA script execution

查看:79
本文介绍了SAP 另存为停止 VBA 脚本执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am writing a VBA code that will save a PDF file from SAP. I've reached the place where SAP asks me where I would like to save my pdf file (opens windows explorer "save as" window).At this point, VBA code stops and I need to manually input the name of the file I want to save. Then, vba continues to run...

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

I need help to find a way to automate this step.

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

A possible solution that I am thinking of(but don't know how to actually do it) is to tell vba to run a VB script that ends at save as window. Then I would send a "application.sendkeys(" ") to input the save as path.

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

Please advise if this is feasible. If it is, next step is I will have to dynamically modify specific lines of the vb script file (I need to loop through a list and change some values every time)

谢谢

推荐答案

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

So, it has been quite a challenge....Here is my solution to Handle a "Save as" window. It can be way simpler if you would only want to click on "Save" Button. My solution is more complicated because I specify where the file needs to be saved. To do that you need to find the right combobox, which takes a lot of iteration.

WinAPI 必要声明:

WinAPI necessary declarations:

    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,结果证明它更简单.

2nd VBA Code : For SAP, as it turns out to be simpler due to ComboboxEx32 being used instead of 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

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

Bottom line, this is not the most perfect code, but I couldn't find anything else on the web. I hope this will benefit anyone with the same problem.

这篇关于SAP 另存为停止 VBA 脚本执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆