I am trying to write a macro to open then print a PDF in Internet Explorer. Eventually I'd like to wait for the print command to be sent then close IE but I'm not there yet.
I've hit a hurdle on sending the print command. Here's my code.
Private Sub PrintPDFInIE()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "PATHTOLOCALFILE"
IE.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER
End Sub
There error I get indicates;
Run-time error '-2147221248 (80040100)':
Method 'ExecWB' of object 'WebBrowser2' failed.
Can someone please shed some light on where I've gone wrong? Many thanks
Update 20181011A:
I've made a slight update to the code changing the IE type from Object to InternetExplorerMedium and using a Do Loop to wait for IE's ready state as per below.
Option Explicit
Private Sub PrintPDFInIE()
Dim IE As InternetExplorer
Set IE = New InternetExplorerMedium
IE.Visible = True
IE.Navigate "PATHTOLOCALPDFFILE"
Do
DoEvents
Loop Until IE.ReadyState = READYSTATE_COMPLETE
IE.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER
End Sub
I'm no longer receiving the error but the print screen isn't triggered unless I put a breakpoint on the IE.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER line. This tells me that there is an issue around waiting for the page to load that I haven't picked up on.
Any advice would be much appreciated.
Bookmarks