Solved

Printing the contents of a webbrowser control

Posted on 2000-04-08
18
200 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
  • 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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 89 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

776 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