Print websites pages without displaying Internet Explorer in VBA

B

BTT

Hi. I got this code from Microsoft website, but it is for VB. Could someone
help me to adapt or add some coding so I can run it from VBA?

Private Sub PrintHelpPage()

' Create a WebBrowser instance.
Dim webBrowserForPrinting As New WebBrowser()

' Add an event handler that prints the document after it loads.
AddHandler webBrowserForPrinting.DocumentCompleted, New _
WebBrowserDocumentCompletedEventHandler(AddressOf PrintDocument)

' Set the Url property to load the document.
webBrowserForPrinting.Url = New Uri("\\myshare\help.html")

End Sub

Private Sub PrintDocument(ByVal sender As Object, _
ByVal e As WebBrowserDocumentCompletedEventArgs)

Dim webBrowserForPrinting As WebBrowser = CType(sender, WebBrowser)

' Print the document now that it is fully loaded.
webBrowserForPrinting.Print()
MessageBox.Show("print")

' Dispose the WebBrowser now that the task is complete.
webBrowserForPrinting.Dispose()

End Sub

Thanks!
 
R

Ralph

The WebBrowser control is part of Visual Studio. You can use Microsoft
Internet Controls to accomplish this. I used a sleep function to wait for the
browser. Kind of klunky.

Sub PrintWebPage()
Const OLECMDID_PRINT = 6
Const OLECMDEXECOPT_PROMPTUSER = 1
Const OLECMDEXECOPT_DONTPROMPTUSER = 2

Dim ie As Object
Dim strWebPage As String

strWebPage = "www.yourwebpagehere.com"
Set ie = CreateObject("internetexplorer.application")

ie.navigate strWebPage

Do Until ie.Busy = False
sSleep (1)
Loop

ie.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER

sSleep (2)

ie.Quit

Set ie = Nothing

End Sub

Sub sSleep(seconds)
Dim ws As Object
Dim cmd
'pause script about 1 second
Set ws = CreateObject("Wscript.Shell")
cmd = "%COMSPEC% /c ping -n " & 1 + seconds & " 127.0.0.1>nul"
ws.Run cmd, 0, 1
End Sub
 
B

BTT

Hi! I am using a very similar one. But instead of using a Sleep Sub I just
put DoEvents inside the "Do Until"...

But do you know any method that Print the Website without even displaying
the WebSite in the screen? (Print a hidden website).

One more thing. I use OLECMDID_PAGESETUP to change the printing to
Landscape. But I have to click it myself. Do you know how to automatically
select the Landscape option?

Thanks!
 
R

Ralph

Somewhere in your code you must be setting Internet Explorer to visible. The
code I posted will print without displaying the page.

There is no setting in SHDocVW to set the printer to landscape, prompting
the user is what is available
 
B

BTT

Thanks Ralph!! It worked here!!

Do you know if I can set the number of pages automatically as well? Or I
have to Prompt first?

Thanks!!!!
 

no1

Joined
Jun 7, 2010
Messages
5
Reaction score
0
Hi I'm just wondering, is it possible in VBA to use the WebBrowser.DocumentComplete event rather than relying on WebBrowser.Busy ?
 
Last edited:

no1

Joined
Jun 7, 2010
Messages
5
Reaction score
0
I would very much like to edit my last post, but the forum won't let me.
I just wanted to say I figured out how to use the DocumentComplete event, I ended up adding a WebBrowser control to the toolbox and then adding it to the form. When I looked back at the form code I was able to make use of various webbrowser events.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top