【问题标题】:Controlling IE 'Save Print Output As' dialogue window in VBA在 VBA 中控制 IE“将打印输出另存为”对话框窗口
【发布时间】:2020-02-19 18:32:24
【问题描述】:

我正在自动化 Internet Explorer 以将 WEB PAGE 打印为 PDF 并使用 VBA 将其保存在桌面上。如何使用 VBA 控制“将打印输出另存为”对话框?我更喜欢没有 Application.SendKeys 的解决方案。

任务是以编程方式捕获此对话框,更改“文件名:”(路径+文件名)。或者,我还想:更改“另存为类型:”。点击保存。

我有“另存为”对话框的工作解决方案,但“将打印输出另存为”似乎不起作用。

【问题讨论】:

  • 你可以试试我详细介绍的方法here。它适用于 64 位。您可以包含您的位版本以帮助指导答案。忽略我对 Selenium 的使用并专注于 API 调用,例如查找窗口
  • 工作就像一个魅力!非常感谢
  • 很高兴听到:-)

标签: excel vba internet-explorer


【解决方案1】:
Option Explicit

Declare PtrSafe Function SendMessageW Lib "User32" (ByVal hWnd As LongPtr, ByVal wMsg As LongPtr, ByVal wParam As LongPtr, ByVal lParam As LongPtr) As LongPtr
Declare PtrSafe Function FindWindowExW Lib "User32" (ByVal hWndParent As LongPtr, Optional ByVal hwndChildAfter As LongPtr, Optional ByVal lpszClass As LongPtr, Optional ByVal lpszWindow As LongPtr) As LongPtr
Public Declare PtrSafe Function FindWindowW Lib "User32" (ByVal lpClassName As LongPtr, Optional ByVal lpWindowName As LongPtr) As LongPtr

Public Const WM_SETTEXT = &HC
Public Const BM_CLICK = &HF5
Public Sub GetInfo()
        Const MAX_WAIT_SEC As Long = 5
        Dim t As Date
        Dim ptrSaveButton As LongPtr
        Dim msg As String
        Dim str1 As String, cls As String, name As String
        Dim ptrSaveWindow As LongPtr
        Dim duiViewWND As LongPtr, directUIHWND As LongPtr
        Dim floatNotifySinkHWND As LongPtr, comboBoxHWND As LongPtr, editHWND As LongPtr

        str1 = "#32770" & vbNullChar

        t = Timer
        Do
            DoEvents
            ptrSaveWindow = FindWindowW(StrPtr(str1))
            If Timer - t > MAX_WAIT_SEC Then Exit Do
        Loop While ptrSaveWindow = 0

        If Not ptrSaveWindow > 0 Then Exit Sub
        duiViewWND = FindWindowExW(ptrSaveWindow, 0&)
        If Not duiViewWND > 0 Then Exit Sub
        directUIHWND = FindWindowExW(duiViewWND, 0&)
        If Not directUIHWND > 0 Then Exit Sub
        floatNotifySinkHWND = FindWindowExW(directUIHWND, 0&)
        If Not floatNotifySinkHWND > 0 Then Exit Sub
        comboBoxHWND = FindWindowExW(floatNotifySinkHWND, 0&)
        If Not comboBoxHWND > 0 Then Exit Sub
        editHWND = FindWindowExW(comboBoxHWND, 0&)
        If Not editHWND > 0 Then Exit Sub


        msg = "C:\Users\ID\Desktop\myTest.pdf" & vbNullChar
        SendMessageW editHWND, WM_SETTEXT, 0, StrPtr(msg)

        cls = "Button" & vbNullChar
        name = "&Save" & vbNullChar
        ptrSaveButton = FindWindowExW(ptrSaveWindow, 0, StrPtr(cls), StrPtr(name))

        SendMessageW ptrSaveButton, BM_CLICK, 0, 0

        Application.Wait Now + TimeSerial(0, 0, 4)
End Sub

【讨论】:

  • 您可以尝试在 24 小时后将您的解决方案标记为该问题的已接受答案,这可能会在未来帮助其他社区成员解决类似问题。感谢您的理解。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-15
  • 1970-01-01
  • 1970-01-01
  • 2016-03-11
  • 2012-05-10
相关资源
最近更新 更多