Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WebBrowser printing

Status
Not open for further replies.

petermeachem

Programmer
Aug 26, 2000
2,270
GB
I have a form with a webbrowser control and a few buttons. I need to print the contents of the webbrowser.

What I have is

Sub PrintPage
...some code
WebBrowser1.Print()
...some more code
end sub

What happens is that the printing doesn't start until the end of the sub. This is no good as the real version prints to a file and ...some more code uses that file. But even using an ordinary printer show the same behaviour.
I have exactly the same problem in Word vba and an Outlook add-on.

I have no idea how to fix this. If anyone could help I would be so grateful

 
You could try a System.Windows.Application.Forms.DoEvents()

^--something like that...

Forces a event clear.

-Sometimes the answer to your question is the hack that works
 
You have something about printing to a file, and other code using that file. What exactly isn't working?

-Sometimes the answer to your question is the hack that works
 
What I was expecting to happen (quite reasonably) was that the print would start as soon as the print statement executed. It isn't, printing doesn't start until the whole button click that calls this sub has finished running code.

This means that my code after the print statement will never work. What I am trying to do is to print a pdf using pdf995 and put the file into a document storage system. Pdf995 has the facility to tell you when it has finished, that is part of my post print code. Problem is that pdf995 doesn't start until way too late. Not a pdf995 problem, exactly the same result with my laser.
 
The print behavior that you describe is normal print behavior. i have seen it before elsewhere.

So, just to reassess, What doesn't work?

-Are there changes made to the doc, that aren't going through, or ARE going through.
-Is it not saving the document off at all?
-Does the doc save, but not print?

David

-Sometimes the answer to your question is the hack that works
 
Let me ask with little words:


What does not work?

-Sometimes the answer to your question is the hack that works
 
I have essentially the same problem - to summarise
1) prepare the html string send it to the webbrowser control and wait for it to load
2) save the default printer and set the printer to the pdf995 printer
3) call webbrowser.print and the print request is issued to the pdf software with the settings set to not ask for the filename etc so it runs invisibly in the background
4) set the default printer back - however because the .print/pdf software seem to pick up the printer straight away the printer changes before it has chance to be picked up so the print ends up on the default printer.

doevents dont seem to have any effect neither putting the .print in a subroutine, no printdone event on the webbrowser that i can see

the only fix i can see is to wait in some way for the print request to be issued to the spooler system before changing from the pdf printer. Messy - sounds like a load of api calls.
Any thoughts anyone?
 
Have you looked at changing the default printer? (Temporarily)

When you print from the built in browser control you don't have a lot of flexibility.

If [blue]you have problems[/blue], I want [green]source code[/green] AND [green]error messages[/green], none of this [red]"there was an error crap"[/red]
 
re: default printer - that was the way i was doing it- should have read
2) save the default printer and set the default printer to the pdf995 printer

no joy doing it that way

I managed to discover a (unpretty) work around - running in debug mode i stopped the code just after the .print expecting it to catch up and create the pdf file - surprisingly it never did!
As i mentioned before leaving the sub & doevents didnt work however if i inserted a msgbox the pdf file was created immediately - looks like the underlying pdf995 code was waiting for an opportunity to jump in and do its processing.
This suits me because the pdf is about to be emailed so a message to the user making sure that they want to email is a reasonable thing.
Havent had chance to test that it behaves the same when deployed.
Thanks for your reply - much appreciated.
 
Follow-up - it looks like it is a time dependent (ie pdf995 running in the background) thing rather than a resources issue.
I tidied up the processing by deleting the pdf file and letting the pdf995 create it and encountered file does not exist & (putting in a 'wait until file exists' check) corrupt pdf file issues.

So it looks like there is no alternative to letting the pdf995 complete its thing - will probably put a wait 2 seconds after the file exists

Cheers
 
Follow-up 2 - after a bit more experimentation i have settled on deleting the pdf file, setting the default printer to pdf995, calling print method on webbrowser (making sure to set the relevant pdf995 options to run silently).

The checking routine consists of -
making sure file exists then looping until file length <> 0 and = last length then trying to open the file using stream reader (to avoid the 'The process cannot access the file blah because it is being used by another process. error message') - code below

tidy up by setting the default printer back to what it was

Dim wrkStreamReader As IO.StreamReader
Dim fileDetail As IO.FileInfo
Dim intoldLength As Integer
' 1 - check that the file exists
Do Until File.Exists(strPDFName)
Application.DoEvents()
Loop

' 2 - make sure file length not zero and not changing
fileDetail = My.Computer.FileSystem.GetFileInfo(strPDFName)
Do Until (fileDetail.Length <> 0 AndAlso fileDetail.Length = intoldLength)
' Sleep(20)
intoldLength = fileDetail.Length
fileDetail = My.Computer.FileSystem.GetFileInfo(strPDFName)
Application.DoEvents()
Loop

' 3- loop around until process finished
Do
Try
wrkStreamReader = New IO.StreamReader(strPDFName, True)
Exit Do
Catch ex As IO.IOException
End Try
Application.DoEvents()
Loop

wrkStreamReader.Close()
wrkStreamReader.Dispose()

 
Good Job. Finally figuring out the little annoyances(sp?) is one of the things I get a kick out of.

If [blue]you have problems[/blue], I want [green]source code[/green] AND [green]error messages[/green], none of this [red]"there was an error crap"[/red]
 
Could have done without the delay whilst i figured it out but hopefully will spare someone else the aggro - hence the posting of the code with comments
thanks for your help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top