Results 1 to 5 of 5

Thread: Sendkeys to the webbrowser object.

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 1999
    Location
    Singapore
    Posts
    32

    Post

    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?

  2. #2
    Addicted Member
    Join Date
    Oct 1999
    Posts
    232

    Post

    Maybe this code help you. It's sample for printing with webbrowser control.

    Code:
    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
    [email protected]
    smalig.tripod.com

  3. #3
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845

    Post

    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?

  4. #4
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744

    Post

    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
    [email protected]
    [email protected]



  5. #5
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845

    Post

    ExecWB only work with IE4 onwards though!


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

    [email protected]
    A BMW Group Company

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width