在VBA中控制IE“将打印输出另存为”对话框窗口

时间:2019-01-28 10:49:09

标签: excel vba internet-explorer

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

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

我有“另存为”对话框的有效解决方案,但对于“将打印输出另存为”似乎无效。

1 个答案:

答案 0 :(得分:0)

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)

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)