?
Solved

Printing the contents of a webbrowser control

Posted on 2000-04-08
18
Medium Priority
?
207 Views
Last Modified: 2010-05-02
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!
0
Comment
Question by:rbudrick
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
  • 4
18 Comments
 
LVL 14

Expert Comment

by:mcrider
ID: 2696405
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
 

Author Comment

by:rbudrick
ID: 2696736
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
 
LVL 14

Expert Comment

by:mcrider
ID: 2697271
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:rbudrick
ID: 2697355
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
 
LVL 14

Expert Comment

by:mcrider
ID: 2697395
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
 

Author Comment

by:rbudrick
ID: 2697429
Adjusted points from 50 to 89
0
 

Author Comment

by:rbudrick
ID: 2697430
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
 
LVL 28

Expert Comment

by:AzraSound
ID: 2698551
hey rob,
it should work.  replace where they have webbrowser1 with the name you gave your web browser control.
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2698552
you know where to find me when you get stuck  =)
0
 

Author Comment

by:rbudrick
ID: 2699099
Oh, that's what I did do...sorry I wasn't clear...that's the first thing I tried...
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2699109
ok and what errors is it giving?  or is it just not working at all?
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2699780
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
 

Author Comment

by:rbudrick
ID: 2700151
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
 

Author Comment

by:rbudrick
ID: 2702587
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
 
LVL 14

Accepted Solution

by:
mcrider earned 356 total points
ID: 2703675
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
 
LVL 28

Expert Comment

by:AzraSound
ID: 2704352
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
 

Author Comment

by:rbudrick
ID: 2704609
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
 
LVL 14

Expert Comment

by:mcrider
ID: 2706124
Thanks for the points! Glad I could help!


Cheers!®©
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question