Link to home
Start Free TrialLog in
Avatar of dannymarfil
dannymarfil

asked on

Print several HTML docs in a WebBrowser

Hi, I want to print some HTML docs, so I load them in a WebBrowser first and then print them, this way:

WebBrowser1.Navigate HTML_Doc1   ' I load the first
WebBrowser1.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER   ' I print the first
WebBrowser1.Navigate HTML_Doc2   ' I load the second
WebBrowser1.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER   ' I print the second
...
WebBrowser1.Navigate HTML_DocN   ' I load the last
WebBrowser1.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER   ' I print the last

It would have to work, but looks like there's some problem. Some HTML docs don't get printed, but if I debug step by step it works fine.
Maybe the WebBrowser can't load all the docs so quickly?

Anyway if you know another way to print several HTML docs in a batch process it will be great!

Thanx!!
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi GeneM,

Well you can do it like this:

Private Enum FilePathStatus
    FileAndPathOK = 0
    PathOK = 1
    FileAndPathBad = 2
End Enum

Private Sub Command1_Click()
    Select Case ValidatePathAndFile(Text1.Text)
    Case FileAndPathOK
        MsgBox "OK"
    Case PathOK
        MsgBox "Bad File"
    Case FileAndPathBad
        MsgBox "Completely stuffed"
    End Select
End Sub

Private Function ValidatePathAndFile(ByVal What As String) As FilePathStatus
    If Dir(What) <> "" Then
        ValidatePathAndFile = FileAndPathOK
    Else
        strPath = Left(What, InStr(What & "\", "\"))
        If GetAttr(strPath) And vbDirectory = vbDirectory Then
            ValidatePathAndFile = PathOK
        Else
            ValidatePathAndFile = FileAndPathBad
        End If
    End If
End Function

Tim Cottee MCSD, MCDBA, CPIM
http://www.timcottee.tk 

Brainbench MVP for Visual Basic
http://www.brainbench.com
Hi, %%QUESTIONER%%

Wrong thread sorry

Tim Cottee MCSD, MCDBA, CPIM
http://www.timcottee.tk 

Brainbench MVP for Visual Basic
http://www.brainbench.com
Hi, %%QUESTIONER%%

You need to wait until the document has finished loading. I would suggest that you place the print instruction in the Document_Complete event of the webbrowser control, this way the document is fully loaded before you attempt to print it.

Tim Cottee MCSD, MCDBA, CPIM
http://www.timcottee.tk 

Brainbench MVP for Visual Basic
http://www.brainbench.com
Avatar of dannymarfil
dannymarfil

ASKER

Yes, Tim, that works for one document but I want to print several in cascade, so I don't know how to load / wait / print and so several times.
ASKER CERTIFIED SOLUTION
Avatar of Marshawk
Marshawk

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for your solution, Marshaw, it works fine.
The problem was that I needed to wait the loading of the html doc before printing it (even though they're local files), and the documentcomplete event is easy for one printing, but it's harder to control it for several printings in cascade...
I've made it with the ReadyState property and it works:

WebBrowser1.Navigate HTML_File
While WebBrowser1.ReadyState <> READYSTATE_COMPLETE
    DoEvents
Wend
Debug.Print WebBrowser1.Document.URL  ' just to check it
WebBrowser1.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER

Thanks!
good deal!