[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 772
  • Last Modified:

How to print specific page(s) with the WebBrowser control & ExecWB

Hi,

I'm using a WebControl inside VB and want to print the web page that's loaded inside it; I use the ExecWB method to bypass the print dialog. Anyway I need to somehow tell it to print only a page range, in my case the first page (on first print) and all the other pages (on second print)

What do you think?
Thanks,
Lazar
0
kerzner
Asked:
kerzner
  • 2
1 Solution
 
junglerover77Commented:
I think that's really possible, but a little complicated.

After the print dialog shows up, you can use "Findwindow" API to get its handle:

lhwnd = FindWindow(vbNullString, "Print")

Then, you can use "FindwindowEx" API to get its child windows (controls), then use "PostMessage" API to post correct Mouse or Keyboard messages to the "Page Range" radio button and text box, so that you can control the page range printed.

Regards,
Jungle
0
 
junglerover77Commented:
Put a webbrowser and 2 buttons on a Form, then test the following code:

Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long

Private Const WM_CHAR = &H102

Private Sub Command1_Click()
    Call WebBrowser1.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DODEFAULT)
End Sub

Private Sub Command2_Click()
    Dim lhWND As Long, lhWND1 As Long, lhWND2 As Long, lhWND3 As String
   
    lhWND = FindWindow(vbNullString, "Print")
    lhWND1 = FindWindowEx(lhWND, 0, vbNullString, "General")
    lhWND2 = FindWindowEx(lhWND1, 0, "#32770", vbNullString)
    lhWND3 = FindWindowEx(lhWND2, 0, "Edit", vbNullString)
   
    Call PostMessage(lhWND3, WM_CHAR, Asc("2"), 1)
End Sub

Private Sub Form_Load()
    Call WebBrowser1.Navigate2("http://www.hotmail.com")
End Sub
0
 
kerznerAuthor Commented:
Thanks for the code.
So that Spy++ tool has its usage:) I was able to check exactly how/why your code works and yes, it works.
I was able to make it clear the textbox, put my letter in, and then pass Alt-P

   PostMessage lhWND3, WM_KEYDOWN, vbKeyDelete, 1
   PostMessage lhWND3, WM_CHAR, Asc("3"), 1
   PostMessage lhWND3, WM_SYSKEYDOWN, Asc("P"), &H20000001  'sends "Alt-P"

OK. It really works. It's not the most elegant solution, though.
I was forced by the client to write it in a way that avoids the user to see the dialog, so I use OLECMDEXECOPT_DONTPROMPTUSER.
However with this code you have to show the print dialog for a while, which is not very good.
I was really looking for a way to set the page range really through some function called on the WebBrowser, without leting the user to see the PrintDialog. However, I realize this doesn't seem quite possible.

Will wait a day or two and then give you the rest of the points (so far you have 200), if I don't get a better solution.

Thanks again,
Lazar
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now