VBA to click within Print Preview window in IE11

2019-08-17 09:06发布

问题:

I would like to write a macro to click the print icon (Alt+P) within print preview window in IE11. I thought of using FindWindow, FindWindowEx, SendMessage and PostMessage functions. But when I checked the window info, there seemed to be no button for clicking the print icon.Print Preview Window Info

Wherever I click within the window, it displays only InternetExplorer_TridentDlgFrame in Class. So how should I click the print icon?

I do not want to use Sendkeys or to perform mouse click based on the location as I would be using this macro in different systems.

So is there any suitable way to do this? Any ideas will be very useful. Thanks in advance.

回答1:

try this code

see here for details https://msdn.microsoft.com/en-us/library/aa752117(v=vs.85).aspx

Option Explicit

Sub printIE()

    Const OLECMDID_PRINT = 6            ' declarations needed for late binding only if mnemonics are desired in ExecWB command
    Const OLECMDID_PRINTPREVIEW = 7

    Const OLECMDEXECOPT_DODEFAULT = 0
    Const OLECMDEXECOPT_PROMPTUSER = 1
    Const OLECMDEXECOPT_DONTPROMPTUSER = 2
    Const OLECMDEXECOPT_SHOWHELP = 3

    Dim IE As Object                    ' late binding

'   Dim IE As InternetExplorer          ' early binding ... reference microsoft internet controls

    Set IE = CreateObject("InternetExplorer.Application")

'   IE.Visible = True
    IE.navigate ("google.co.in")

    Do Until IE.ReadyState = 4
    Loop

    IE.ExecWB OLECMDID_PRINTPREVIEW, OLECMDEXECOPT_DONTPROMPTUSER

'   IE.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER

End Sub