0

I am automating Internet Explorer to print WEB PAGE to PDF and save it on Desktop with VBA. How to control with VBA "Save Print Output As" dialogue box? I would prefer solution without Application.SendKeys.

The task is to catch this dialog programmatically, change the "File name:" (path + file name). Optionally, I would also like to: Change "Save as type:". Click on Save.

I have working solution with "Save As" dialogue but for "Save Print Output As" it seems not working.

halfer
  • 19,824
  • 17
  • 99
  • 186
kabarto
  • 75
  • 1
  • 14
  • 2
    you can try the approach I detail [here](https://stackoverflow.com/questions/52890015/vba-ie-automation-save-as-pdf-isnt-working/52906215#52906215). It is for 64 bit. You could include your bit version to help guide answers. Ignore my use of Selenium and focus on the API calls e.g. FindWindow – QHarr Jan 28 '19 at 11:00
  • working like a charm! thx a lot – kabarto Jan 28 '19 at 13:55
  • Good to hear :-) – QHarr Jan 28 '19 at 13:56

1 Answers1

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)
End Sub
Teamothy
  • 2,000
  • 3
  • 16
  • 26
kabarto
  • 75
  • 1
  • 14
  • You can try to mark your solution as an accepted answer for this question after 24 hrs may help other community members in future in similar kind of questions. Thanks for your understanding. – Deepak-MSFT Jan 29 '19 at 02:35