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!

Printing a HTML string

Status
Not open for further replies.

Mungovan

Programmer
Oct 24, 2002
94
IE
Hi.
I have a string called sFrag that contain HTML code of the form
sFrag = &quot; <TABLE> <TR> <B> Hello World ........ <\TABLE> &quot;

I need this to be printed to the printer in its table format. But when I try Printer.Print sFrag it just prints the characters and text (and not in the table format I want)

Any ideas of how to print it so it's in its nice formatted Table HTML format.


Anyway, thanks for the help,

D
 
To print HTML you can use the Webbrowser control and the ExecWB method. Intellisense will bring up a list of the arguments. Try:

wb1.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_PROMPTUSER


________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Thanks for the reply.

I tried the above line and I got the error that the variable &quot;OLECMDID_PRINT&quot; was not defined?

Any ideas?



 
To use the WebBrowser control you will need to add The MS Internet controls to the project, and add a webbrowser control to your form. The code given assumes the WebBrowser control is called wb1.

I've just run a one-form project with just a command button, a webbrowser contol on the form, and it works fine. I suppose we are talking about VB6 SP5?

The whole project code is:
Private Sub Command1_Click()
wb1.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_PROMPTUSER
End Sub

Private Sub Form_Load()
wb1.Navigate &quot;End Sub



________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Thanks for the reply.

How does this print the contents of my string sFrag to the Printer??
 
Cool.
Think I get what you're sayin. I have the webBrowser on my project. But do you know what is the command to add the HTML text onto the webBroser?

Thanks a million
D
 
Just point the browser to your html file:

wb1.Navigate &quot;c:\windows\web\tip.htm&quot;


________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Thanks.

But my HTML code is a string [sFrag = &quot;<table>.....&quot; ], not a web page.

Will wb1.Navigate &quot;sFrag&quot; work in this case??

Thanks,
D

 
Can't really help you - I would just write to a file then use that - but there must be a better way!

Dim ff As Integer
ff = FreeFile
Open &quot;c:\ht.htm&quot; For Output As ff
Print #ff, strA
Close ff
wb1.Navigate &quot;c:\ht.htm&quot;


________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Hi.
My application opens up a HTML file and writes a srting to it. Does anyone know the command to print the contents of the HTML file to the printer.

My code is:
Dim ff As Integer
ff = FreeFile
Open App.Path & &quot;\&quot; & &quot;myFile.html&quot; For Output As ff
Print #ff, sFrag ' Send the string to the file

' Now I need to print the HTML file to the printer.

Thanks,
D
 
as johnwm says call that file in a web browser control and use this:
wb1.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_PROMPTUSER
where wb1 is the control name....

Known is handfull, Unknown is worldfull
 
I'm probably doing something simple wrong!
I have tried this method and have set up the webbrowser properly and am using the correct commands.

WebBrowser2.Navigate App.Path & &quot;\&quot; & &quot;Print.html&quot;
WebBrowser2.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_PROMPTUSER

But when I call the Sub I'm getting an error saying:
Method 'ExecWB' of object IWebBrowser2 failed.

The webBrowser that I'm using is called WebBrowser2.

Any Ideas??

Thanks
D
 
just try this:
this line:
WebBrowser2.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_PROMPTUSER

remove it immedeately after this line:
WebBrowser2.Navigate App.Path & &quot;\&quot; & &quot;Print.html&quot;

and put it inside a command button called &quot;print&quot; (add one command box). see if it works.



Known is handfull, Unknown is worldfull
 

Created the command button print. Put the code into Print_Click() but I'm still getting the error.

Even if it did work, would the user have to click on this new Print button as well??

Thanks,
D
 
no thats just for checking purpose.

does the page load in the browser control?
and one more thing:
type this (u must get a dropdown for all possible actions of which OLECMDEXECOPT_PROMPTUSER is one, try different options:
WebBrowser2.ExecWB OLECMDID_PRINT,

Known is handfull, Unknown is worldfull
 
Along with the webbrowser control, add a reference to the Microsoft HTML Object Library. Then, sticking with the ExecWB printing solution:
[tt]
Option Explicit
Private GotPage As Boolean

Private Sub Command1_Click()
PrintFragmentHTML &quot;<table border='1' cellpadding='0' cellspacing='0' width='124' height='42'><tr><td width='63' height='42'><b>Hello</b></td> <td width='61' height='42'><b>World</b></td></tr> </table>&quot;
End Sub

Private Sub PrintFragmentHTML(strFragment As String)
Dim InsertHTML As IHTMLDocument2
Set InsertHTML = WebBrowser1.Document
InsertHTML.body.innerHTML = strFragment
WebBrowser1.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_PROMPTUSER
End Sub


Private Sub Form_Load()
GotPage = False
WebBrowser1.Navigate &quot;about:blank&quot;
' Let navigation complete
Do Until GotPage
DoEvents
Loop
End Sub

Private Sub WebBrowser1_NavigateComplete2(ByVal pDisp As Object, URL As Variant)
GotPage = True
End Sub
 
Hi.
Thanks for the help.
The application no longer shows any run time errors. The only problem is that it prints a blank internet page &quot;The page cannot be displayed&quot;

It says on the page that it &quot;Cannot find the server or DNS Error&quot;

Any ideas,
D
 
Hi.
Thanks for the help.
The application no longer shows any run time errors.
I'm using the code:

WebBrowser1.Navigate App.Path & &quot;\&quot; & &quot;Print.html&quot;
WebBrowser1.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_PROMPTUSER

Where WebBrowser1 is the inserted webbrowser and Print is the HTML page I want to print.

The only problem is that it prints a blank internet page &quot;The page cannot be displayed&quot;

It says on the page that it &quot;Cannot find the server or DNS Error&quot;

Any ideas,
D
 
do this:
msgbox (App.Path & &quot;\&quot; & &quot;Print.html&quot;)

does this give the right path?

if the html file is in the same folder try this:

WebBrowser1.Navigate &quot;Print.html&quot; 'i am not sure if this will work.


Known is handfull, Unknown is worldfull
 
The path is right with the message box.
The webbrowser is &quot;Behind&quot; the main form so I can't move/ delete it to see what's inside.

Also, I created another webbrowser, pointedf the html file at it to see what's inside it, but I can only place webbrowsers on the toolbar part and, as such, it's too 'skinny' to see what's inside it.

Thanks,
D
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top