Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 696
  • Last Modified:

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!!
0
dannymarfil
Asked:
dannymarfil
  • 3
  • 2
  • 2
1 Solution
 
TimCotteeCommented:
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
0
 
TimCotteeCommented:
Hi, %%QUESTIONER%%

Wrong thread sorry

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

Brainbench MVP for Visual Basic
http://www.brainbench.com
0
 
TimCotteeCommented:
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
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
dannymarfilAuthor Commented:
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.
0
 
MarshawkCommented:
See if this does the trick: After each navigate do a loop on the readyState. That way the complete HTMLDocument object should load.

WebBrowser1.Navigate HTML_Doc1   ' I load the first

While WebBrowser1.readyState <> READYSTATE_COMPLETE
  DoEvents
Wend

WebBrowser1.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER   ' I print the first


Then use the same code structure for the other two.



TimCottee's documentcomplete suggestion should work fine as well.

Private Sub oBrowser_DocumentComplete(ByVal pDisp As Object, Url As Variant)

pDisp.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER
End Sub

There are a few gotcha's with the documentcomplete, especially if you have embedded iframes in your document and are using IE6. Basically its a good idea to check that pDisp is an HTMLDocument using typename() before using it.
 
0
 
dannymarfilAuthor Commented:
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!
0
 
MarshawkCommented:
good deal!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now