This was suggested to me by Meyeth and it worked:
To print the (contents of a webbrowser) control, simply follow the steps
below to set focus to the correct window and initiate the SendKeys
function:
Start a new standard Exe project in Visual Basic. Form1 is created by
default.
Add the "Microsoft Internet Controls" (shdocvw.dll) to the project.
Place the WebBrowser control on the default form (Form1).
Add the following code to load the default page:
Private Sub Form_Load()
WebBrowser1.Navigate "http://www.microsoft.com"
End Sub
Place a CommandButton on the form and change its caption to "Print." Add
the following code to the Click event of the button:
Private Sub Command1_Click()
Dim hwnd As Long
WebBrowser1.SetFocus
hwnd = GetFocus
SetFocusToBrowser (hwnd)
SendKeys "^p" 'CTRL+P to print
End Sub
Add a module to the project, and then add the following code to it:
Option Explicit
Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, _
ByVal wCmd As Long) As Long
Declare Function GetWindowLong Lib "user32" _
Alias "GetWindowLongA" (ByVal hwnd As Long, _
ByVal nIndex As Long) As Long
Declare Function SetFocusAPI Lib "user32" _
Alias "SetFocus" (ByVal hwnd As Long) As Long
Declare Function GetFocus Lib "user32" () As Long
Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" (ByVal hwnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, _
lParam As Long) As Long
'GetWindow constants
Public Const GW_CHILD = 5
'GetWindowLong constants
Public Const GWL_STYLE = (-16)
Public 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
Run the project, and click Print.
This was suggested to me by Meyeth and it worked:
To print the (contents of a webbrowser) control, simply follow the steps
below to set focus to the correct window and initiate the SendKeys
function:
Start a new standard Exe project in Visual Basic. Form1 is created by
default.
Add the "Microsoft Internet Controls" (shdocvw.dll) to the project.
Place the WebBrowser control on the default form (Form1).
Add the following code to load the default page:
Private Sub Form_Load()
WebBrowser1.Navigate "http://www.microsoft.com"
End Sub
Place a CommandButton on the form and change its caption to "Print." Add
the following code to the Click event of the button:
Private Sub Command1_Click()
Dim hwnd As Long
WebBrowser1.SetFocus
hwnd = GetFocus
SetFocusToBrowser (hwnd)
SendKeys "^p" 'CTRL+P to print
End Sub
Add a module to the project, and then add the following code to it:
Option Explicit
Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, _
ByVal wCmd As Long) As Long
Declare Function GetWindowLong Lib "user32" _
Alias "GetWindowLongA" (ByVal hwnd As Long, _
ByVal nIndex As Long) As Long
Declare Function SetFocusAPI Lib "user32" _
Alias "SetFocus" (ByVal hwnd As Long) As Long
Declare Function GetFocus Lib "user32" () As Long
Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" (ByVal hwnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, _
lParam As Long) As Long
'GetWindow constants
Public Const GW_CHILD = 5
'GetWindowLong constants
Public Const GWL_STYLE = (-16)
Public 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
Run the project, and click Print.