Printing the contents of a webbrowser control

Hi there!

(first just a note to say thank God [EE] for allowing some points for filling out the survey! :-)  )

I have a webbrowser control in my form, and I want to print the contents of it.  Of course the PrintForm method prints the whole form, so that's no help.  So, if the webbrowser control brings up a page, how can I get it to print just the page in the webbrowser?

Thanks!
rbudrickAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
mcriderConnect With a Mentor Commented:
Make sure the "SendKeys" statement looks like this:

    SendKeys "^p"

     -NOT-

    SendKeys "^P"

If you send an uppercase "P" it will not work...


You can also use the ExecWB method to print... This will print to the default printer without asking the user:

    On Error Resume Next
    WebBrowser1.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER


And this will prompt the user:

    On Error Resume Next
    WebBrowser1.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_PROMPTUSER



Cheers!®©
0
 
mcriderCommented:
See the following microsoft KB article:

HOWTO: Print the WebBrowser Control
http://support.microsoft.com/support/kb/articles/Q176/4/00.ASP?LNG=ENG&SA=MSDN&FR=0 


Cheers!®©
0
 
rbudrickAuthor Commented:
When I use this routine, run the project, and then press the print button, the classic Windows "ding" bell sound occurs.  No error message appears, but this doesn't seem to work...not sure what I'm doing wrong...
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
mcriderCommented:
Did you try the example exactly as it is shown in the article??  It works perfectly on all of my development systems (95/98/NT4.0)

What version of VB are you using?  If you are using VB 5 or 6, do you have Service Pack 3 installed??  If not, you should... It fixes several problems with VB...


Cheers!®©
0
 
rbudrickAuthor Commented:
Ive got vb 6....one prob I had is that it wanted to know a default page....do I have to have put in one of those? I don't want the user to be forced to have any given default page.  (especially when all they will see is offline...it is a collection of articles on a CD)

And yeah, I did put it in exactly as it looks with the exception that my web browser control is called webbrows, not WebBrowser1...that's the only thing I changed....hmmm  not sure..I do know that when I right-click on the webbrowser control, I have the option to make it print, but I wanted the button too
0
 
mcriderCommented:
Create the demo *exactly* as it appears in the MS article and run it... If it doesn't work, you've got a bigger problem.


Cheers!®©
0
 
rbudrickAuthor Commented:
Adjusted points from 50 to 89
0
 
rbudrickAuthor Commented:
I did it exactly, and it gave me an error that Webbrower1 was not a defined variable.  It wanted the real name, webbrows.   Any other ideas?
0
 
AzraSoundCommented:
hey rob,
it should work.  replace where they have webbrowser1 with the name you gave your web browser control.
0
 
AzraSoundCommented:
you know where to find me when you get stuck  =)
0
 
rbudrickAuthor Commented:
Oh, that's what I did do...sorry I wasn't clear...that's the first thing I tried...
0
 
AzraSoundCommented:
ok and what errors is it giving?  or is it just not working at all?
0
 
mcriderCommented:
OK... Lets start from scratch.

1) start a new project.

2) Add a webbrowser control to Form1.  Don't change the name of the webbrowser control.

3) Add a command button to Form1.  Don't change the name of the command button.

4) Add the following code to the "Declarations Section" of form1:


'-----------------------------------------------------------------------------------------
      Option Explicit
      Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, _
          ByVal wCmd As Long) As Long
      Private Declare Function GetWindowLong Lib "user32" _
          Alias "GetWindowLongA" (ByVal hwnd As Long, _
          ByVal nIndex As Long) As Long
      Private Declare Function SetFocusAPI Lib "user32" _
          Alias "SetFocus" (ByVal hwnd As Long) As Long
      Private Declare Function GetFocus Lib "user32" () As Long
      Private Declare Function SendMessage Lib "user32" _
          Alias "SendMessageA" (ByVal hwnd As Long, _
          ByVal wMsg As Long, ByVal wParam As Long, _
          lParam As Long) As Long

      Private Const GW_CHILD = 5
      Private Const GWL_STYLE = (-16)
      Private Const WS_VSCROLL = &H200000
      Sub SetFocusToBrowser(hBrowserHwnd As Long)
          Dim lStyle As Long
          Dim lResult As Long
          Dim hwnd As Long
          hwnd = hBrowserHwnd
          While (lResult = 0) And (hwnd <> 0)
              hwnd = GetWindow(hwnd, GW_CHILD)
              lStyle = GetWindowLong(hwnd, GWL_STYLE)
              lResult = lStyle And WS_VSCROLL
          Wend
          SetFocusAPI (hwnd)
      End Sub
      Private Sub Command1_Click()
          Dim hwnd As Long
          WebBrowser1.SetFocus
          hwnd = GetFocus
          SetFocusToBrowser hwnd
          SendKeys "^p"
      End Sub
      Private Sub Form_Load()
          WebBrowser1.Navigate "http://www.microsoft.com"
      End Sub
'-----------------------------------------------------------------------------------------



5) Run the program and wait for the microsoft web page to load in the webbrowser control.

6) Click the Command1 button.  A Print dialog box will appear.

7) Click the OK button and the web page will print.



This is *Guaranteed* to work... ;-)



Cheers!®©
0
 
rbudrickAuthor Commented:
Yeah, when I make a new project it works fine...but it doesn't work in the one I have.  When I change the webbrowser1 name to my name, webbrows, it doesn't give any error, just a "ding."  Even if I go thgrough the whole project and change all references to the webbrowser control to the name given in the example, it still just gives me the bell sound....very strange.
0
 
rbudrickAuthor Commented:
One problem may be that, I dont have a default page...you see, the project is an article database, and when the user does a search, and finds the article he/she wants, he/she clicks on the link, and then the form with tthe webbrowser appears and displays the article.  Do I really need to have a default page?  I put one in like the example showed, but it still didn't work.
0
 
AzraSoundCommented:
hey mcrider,
I've been helping rob out with this project for a couple of weeks now and we got it working last night.  I dont know what he was doing wrong but when I wrote it out and sent it back to him it worked fine.  
0
 
rbudrickAuthor Commented:
Well mcrider...all works now (Azra has saved my butt a couple times with this project!!!)  I have no clue what was going wrong...I know I did everything as it was supposed to, but as I mentioned to Azra, my computer sucks!  Maybe it just wanted to work on someone else's machine first :-)

Thanks a bunch!!
0
 
mcriderCommented:
Thanks for the points! Glad I could help!


Cheers!®©
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.