PDA

Click to See Complete Forum and Search --> : Sendkeys to the webbrowser object.


Athenis
Nov 2nd, 1999, 07:47 PM
Hi,
How do I focus the webbrowser object so I can send keystrokes to it?
I'm trying to write this app that enters login info for me on a form..
I tried to focus the VB form, then use Sendkeys "{Tab}..." to get the the form elements.. but somehow, it just doesn't work~
I've even tried to create a form with no other controls.. but still I can get the webbrowser object to receive focus.

Neither did webbrowser.setfocus works..
So, how do I get the object to fill up a particular field or press {enter} at a particular button? I'm prepared to count the number of tabs to get to the particular HTML object displayed if it actually works..
What did I do wrong?

smalig
Nov 2nd, 1999, 08:34 PM
Maybe this code help you. It's sample for printing with webbrowser control.


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

Private Sub Command1_Click()

Dim hwnd As Long

WebBrowser1.SetFocus
hwnd = GetFocus
SetFocusToBrowser (hwnd)
SendKeys "^P" 'Control-P to print

End Sub


Best regards.

------------------
smalig
smalig@hotmail.com
smalig.tripod.com (http://smalig.tripod.com)

Mark Sreeves
Nov 2nd, 1999, 08:49 PM
to send keys tab to the elements within html in a webbrowser control do this:

Option Explicit
Dim arrTabStop() As Boolean

Private Sub WebBrowser1_GotFocus()
Dim i As Integer
'Store the TabStop property for each control on the
'form and then set the TabStop property of each
'control to False
ReDim arrTabStop(0 To Controls.Count - 1) As Boolean
For i = 0 To Controls.Count - 1
arrTabStop(i) = Controls(i).TabStop
Controls(i).TabStop = False
Next
End Sub

Private Sub WebBrowser1_LostFocus()

Dim i As Integer
'Restore the Tabstop property for each control on the form
For i = 0 To Controls.Count - 1
Controls(i).TabStop = arrTabStop(i)
Next
End Sub

Private Sub Command1_Click()
'this will tab through the elements within 'the html
WebBrowser1.SetFocus
SendKeys "{Tab}"
End Sub


is this what you wanted to do?

Serge
Nov 2nd, 1999, 10:25 PM
To print contents of a WebBrowser you don't have to use that long routine, you can achieve it with 1 line of code:

Without showing the Print Dialog Box
WebBrowser1.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER

With the Print Dialog Box
WebBrowser1.ExecWB OLECMDID_PRINT,OLECMDEXECOPT_PROMPTUSER


Regards,


------------------

Serge

Software Developer
Serge_Dymkov@vertexinc.com
Access8484@aol.com

Mark Sreeves
Nov 2nd, 1999, 10:27 PM
ExecWB only work with IE4 onwards though!


------------------
Mark Sreeves
Analyst Programmer

Mark.Sreeves@Softlab.co.uk
A BMW Group Company