VBA: Auto fill in form of open website

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Arne Vanhoof
    New Member
    • Apr 2011
    • 3

    VBA: Auto fill in form of open website

    Dear all,

    I already searched and found a lot solutions to navigate to a page by vba and fill in form controls.
    However, I want to fill in form controls of an open window in IE, but I have no idea how to appoint the open IE window to use in VBA code.

    Does anyone has an idea how to solve this issue?

    Thanks in advance
  • pod
    Contributor
    • Sep 2007
    • 298

    #2
    hope this helps a bit. Here is some code I use to get to a web page c/w frames and set a value to a field and submit the form to get the results...

    Code:
    'finds an open IE site by checking the URL
    Function GetOpenIEByURL(hvid As Long) As SHDocVw.WebBrowser
        'hvid is the value I want to send in the form
        Dim flag As Boolean
        flag = False
        Dim i_URL As String
        Dim objIE As Object
        i_URL = "http://some.website.com/frames.asp"
        Dim objShellWindows As New SHDocVw.ShellWindows
        'some error handling
        On Error GoTo catchError
        'loop over all Shell-Windows
        Dim IEframes As FramesCollection
        For Each GetOpenIEByURL In objShellWindows
            'if the document is of type HTMLDocument, it is an IE window
            If TypeName(GetOpenIEByURL.Document) = "HTMLDocument" Then
                'check the URL
                If InStr(GetOpenIEByURL.Document.url, i_URL) > 0 Then
                    Call finditforme(i_URL)
                    'I capture the outer frame
                    Set IEframes = GetOpenIEByURL.Document.frames
                    'I capture the inner frame
                    Set IEframes = IEframes.Item(0)
                    'now I want to capture the form and its elements and set them for submital
                    IEframes(0).Document.Forms(0).SearchText.Value = hvid
                    IEframes(0).Document.Forms(0).SearchWhat.selectedIndex = 1
                    IEframes(0).Document.Forms(0).HVQS_ACTION.Value = 1
                    IEframes(0).Document.Forms(0).submit
                    flag = True
                    Call finditforme(i_URL)
                    Exit For
    ''            Else
    ''                MsgBox "Please login and try again"
                End If
            End If
        Next
        If Not flag Then
            MsgBox "Please login and try again"
            Call GetNewIE("http://some.website.com/frames.asp")
        End If
        Exit Function
    catchError:
        MsgBox Err.Description
    End Function
    
    Sub GetNewIE(iurl)
        Dim objIE As Object
        On Error GoTo error_handler
        Set objIE = CreateObject("InternetExplorer.Application")
            With objIE
                .Navigate iurl
                Do While .Busy: DoEvents: Loop
                Do While .ReadyState <> 4: DoEvents: Loop
                .Visible = True
            End With
        Set objIE = Nothing
        Exit Sub
    error_handler:
        MsgBox ("Unexpected Error, I'm quitting.")
        objIE.Quit
        Set objIE = Nothing
    End Sub

    Comment

    Working...