# Using a macro to open then print a webpage?



## pbroadmeadow (Dec 3, 2011)

Hello all!  I am trying to use a macro in Excel to open a website (the URL is a named range in a worksheet) and then print the web page.  I am able to open the webpage with the following code:

Sub View_Tech_Recalls()
Set ie = CreateObject("InternetExplorer.Application")
ie.navigate Range("Recall_URL")
ie.StatusBar = False
ie.Toolbar = True
ie.Visible = True
ie.Resizable = True
ie.AddressBar = False
End Sub

What do I need to add in order to print the open web page?

Thanks for your help!

Paul


----------



## Warship (Dec 3, 2011)

Welcome to the board

```
ie.ExecWB 6, 2 'print
Application.Wait (Now + TimeValue("0:00:03")) 'let print finish befor continue
```

Google ExecWB for more info


----------



## pbroadmeadow (Dec 3, 2011)

Hi Warlock,
I added the line of code you recommended; IE opens to the website but the following error is reported:

Run-time error '-2147221248 (80040100)':
Method "ExecWB of object 'IWebBrowser2' failed

Selecting Debug highlights 

Sub View_Tech_Recalls()
Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate Range("Recall_URL")
ie.StatusBar = False
ie.Toolbar = True
ie.Visible = True
ie.Resizable = True
ie.AddressBar = False
*ie.ExecWB 6, 2 'print
*Application.Wait (Now + TimeValue("0:00:03")) 'let print finish befor continue
End Sub

Thanks again for your help.

Paul


----------



## Warship (Dec 3, 2011)

We also need to be sure IE is fully loaded before printing.

```
Sub View_Tech_Recalls()
    Set ie = CreateObject("InternetExplorer.Application")
    ie.navigate Range("Recall_URL")
    ie.StatusBar = False
    ie.Toolbar = True
    ie.Visible = True
    ie.Resizable = True
    ie.AddressBar = False
    
    
    TimeOutWebQuery = 5
    TimeOutTime = DateAdd("s", TimeOutWebQuery, Now)
    Do Until ie.readyState = 4
        DoEvents
        If Now > TimeOutTime Then
            ie.stop
            GoTo ErrorTimeOut
        End If
    Loop



    ie.ExecWB 6, 2
    Application.Wait (Now + TimeValue("0:00:03"))
    
    

ErrorTimeOut:

    
    Set ie = Nothing
End Sub
```


----------



## pbroadmeadow (Dec 3, 2011)

Awesome!  Worked well; thanks very much for sharing your experience!

Paul


----------



## Warship (Dec 3, 2011)

No problem at all, you're quite welcome.
Stick around MrExcel and you'l find plenty of tips/tricks!


----------



## aubol (May 6, 2015)

I am also trying to batch print a series of webpages / URLs that I have in a column in my Excel sheet. The URLs are in column A ... can you provide a bit more of a tutorial? I believe I am having a problem with the range;   ie.navigate Range("A1:A7")  . I am getting a run time error '13' type mismatch. Thank you in advance.


----------



## endwood (May 27, 2015)

What about changing this macro to open firefox instead? Can someone give feedback on how to do that?


----------



## Watchdawg (Aug 15, 2018)

Hey Warship, following you has gotten me as close as anything so far on my issue.  It's much the same as this issue, however, in my case the same failure did not go away.  My module is supposed to print a pdf that pulls up in internet explorer.  It pulls the pdf up with no issue at all, but then halts at IE.ExecWB 6, 2.  It's supposed to print the PDF without prompting the user for any additional input and then close the browser, but this is as far as I've gotten.  Here's a the module:

              Const OLECMDID_PRINT = 6
Const OLECMDEXECOPT_DONTPROMPTUSER = 1
Const PRINT_WAITFORCOMPLETION = 2
Sub Sample(addr As String)
Dim IE As InternetExplorerMedium
Set IE = New InternetExplorerMedium
IE.Navigate addr
IE.Visible = 1

Do While IE.Busy
  DoEvents
Loop
IE.ExecWB 6, 2 
Application.Wait (Now + TimeValue("0:00:03"))
'OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER
ErrorTimeOut:
    Set IE = Nothing

End Sub


----------



## farhan08 (Apr 21, 2019)

pbroadmeadow said:


> Awesome!  Worked well; thanks very much for sharing your experience!
> 
> Paul



Can you guide how you setup *IE.navigate Range("Recall_URL") *on excel worksheet? cuz its giving me Type Mismatch error.


----------



## pbroadmeadow (Dec 3, 2011)

Hello all!  I am trying to use a macro in Excel to open a website (the URL is a named range in a worksheet) and then print the web page.  I am able to open the webpage with the following code:

Sub View_Tech_Recalls()
Set ie = CreateObject("InternetExplorer.Application")
ie.navigate Range("Recall_URL")
ie.StatusBar = False
ie.Toolbar = True
ie.Visible = True
ie.Resizable = True
ie.AddressBar = False
End Sub

What do I need to add in order to print the open web page?

Thanks for your help!

Paul


----------



## Macropod (Apr 21, 2019)

farhan08: Kindly don't resurrect long-since dormant threads just to hijack it with an unrelated question. Start a new thread instead. Thread closed.


----------

