rbudrick
asked on
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!
(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!
ASKER
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...
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!®©
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!®©
ASKER
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
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
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!®©
Cheers!®©
ASKER
Adjusted points from 50 to 89
ASKER
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?
hey rob,
it should work. replace where they have webbrowser1 with the name you gave your web browser control.
it should work. replace where they have webbrowser1 with the name you gave your web browser control.
you know where to find me when you get stuck =)
ASKER
Oh, that's what I did do...sorry I wasn't clear...that's the first thing I tried...
ok and what errors is it giving? or is it just not working at all?
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(hBrowser Hwnd 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!®©
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(hBrowser
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!®©
ASKER
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.
ASKER
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
ASKER
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!!
Thanks a bunch!!
Thanks for the points! Glad I could help!
Cheers!®©
Cheers!®©
HOWTO: Print the WebBrowser Control
http://support.microsoft.com/support/kb/articles/Q176/4/00.ASP?LNG=ENG&SA=MSDN&FR=0
Cheers!®©