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

Posted on 2006-05-11
Last Modified: 2012-06-27

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?
Question by:kerzner
    LVL 6

    Expert Comment

    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.

    LVL 6

    Accepted Solution

    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()
    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("")
    End Sub
    LVL 1

    Author Comment

    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,

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    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…
    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…

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now