webbrowser control

linus

New Member
Joined
Nov 21, 2005
Messages
2
For some time I have viewed this message board and thank you. I however need some advice on how to gain control of particular controls on a web page that I navigate to within a userform. It is a login webpage and I know the names of the controls but I get a runtime error91 when I try to access them. I have tried assigning values to them by creating an object and then assigning a value but no luck. My reason for this is that I want to direct users within our organization to navigate part of our website but not all without gaining knowledge of the username and password. Within the userform I was able to use a

Private Sub WebBrowser1_NavigateComplete2(ByVal pDisp As Object, URL As Variant), which allowed me to monitor if a page was selected and hence close the application.

My code to date is as follows and any help will be appreciated...

Sub CallUserForm()
UserForm.Show
With UserForm.WebBrowser1
.Navigate "http://..."
End With
End Sub
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Thanks for the quick response.

I added the following code and when I step through the process it works great but when I run it I still get the runtime error 91.

With UserForm.WebBrowser1.Document.all
.username.Value = "****"
.password.Value = "****"
.login.Click
End With

Any ideas why this is failing
 
Upvote 0
This is soooo not my forté! It may be that you're not getting a proper handle to the button object you seek. What I have done myself is loop through all of the objects on a page and see if they match certain criteria and if so, set a variable to them so that I can later .click them or whatever. Something like...

Code:
Option Explicit

Public g_booUsingProgressIndicator As Boolean

Private Const mc_intMaxAWBcanSubmit As Integer = 25
Private Const mc_strLoomisTrackURL As String = "http://www.dhl.ca/tracking/"
Private Const mc_strTempFile As String = "C:\temp.html"
Private Const mc_strJavaSubmit As String = "javascript:check_num_waybills('en');"
Private Const mc_strJavaNext As String = _
          "javascript:document.content.B1.value='Next';%20document.content.submit();"

Public Declare Function ShowWindow& Lib "user32" _
    (ByVal hwnd As Long, ByVal nCmdShow As Integer)


'¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Sub TrackSelection()
'______________________________________________________________________________

    g_booUsingProgressIndicator = False
    TrackLoomis Selection, , True
End Sub



'¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Sub TrackLoomis(Optional rngInput As Range, _
                Optional bytOutputCol As Byte = 0, _
                Optional booPromptToShow As Boolean = False)
'______________________________________________________________________________

    
    Dim appIE As SHDocVw.InternetExplorer
    Dim objDoc As MSHTML.HTMLDocument
    Dim objCuerpo As MSHTML.HTMLBody
    Dim objBody As MSHTML.IHTMLBodyElement
    
    Dim objTxtRange As IHTMLTxtRange
    Dim objLink As MSHTML.IHTMLLinkElement
    Dim objAnch As MSHTML.HTMLAnchorElement
    Dim objInputElem As MSHTML.HTMLInputElement
    Dim objGen As Object
....
    Set appIE = New InternetExplorer
    Set objDO = New DataObject
    Set fso = New FileSystemObject
    Set wsActive = ActiveSheet
....
    With appIE
        .navigate mc_strLoomisTrackURL
        Application.StatusBar = "Navigating to " & mc_strLoomisTrackURL & ", Busy: "
        Do While .Busy: GoSub DoThemEvents: Loop
        Application.StatusBar = Replace(Application.StatusBar, "Busy", "Ready")
        lngBusy = 0
        Do Until .readyState = READYSTATE_COMPLETE: GoSub DoThemEvents: Loop
        Set objDoc = .document
    End With
    
    Application.StatusBar = "Scanning elements"
    For Each objGen In objDoc.all
        If TypeName(objGen) = "HTMLAnchorElement" Then
            If objGen.href = mc_strJavaSubmit Then Set objAnch = objGen
        End If
    Next objGen
    
    strAWBlist = ConvertToList(rngInput)
    strAWBlist = Replace(strAWBlist, " ", "")
    Application.StatusBar = "Entering AWB(s) & ""clicking""."
    With objDoc.forms(0)
        .Sa_waybill_list.Value = strAWBlist
    End With
    objAnch.Click
    Set objAnch = Nothing
    Application.StatusBar = "Waiting for response."
...

Hope that helps!
 
Upvote 0
Hello,

Pardon the interruption... :)
linus said:
I added the following code and when I step through the process it works great but when I run it I still get the runtime error 91.

Code:
With UserForm.WebBrowser1.Document.all
        .username.Value = "****" 
        .password.Value = "****" 
        .login.Click
End With

Any ideas why this is failing
To state the issue, you're passing information to a controls in a specific form in the document, you want to reference a Form, like in the examples presented to you. You might be able to use 'All', but not like that...

E.g.,

Code:
    With .Document.Forms(0)
        .UserName.Value = "Your Login ID" 'Change to YOUR Login ID
        .Password.Value = "Your Password" 'Change to YOUR password
        .login.Click
    End With
You also need to look at the source to make sure the name of the Controls is correct. In the above example, there is a textbox named 'Username' and a textbox named 'Password' in the first Form of the Document (Forms(0)).

All of this needs to be specific and correct in order to work, don't assume which form it is in the document, or what the control names are, look at the source.
 
Upvote 0
'bout bloody time ya got yer tuckus outta the lounge and showed up and saved the day! Heaven knows we could use someone on this thread who knows what the heck he's doin' 'cause t'aint me!
 
Upvote 0

Forum statistics

Threads
1,214,625
Messages
6,120,598
Members
448,973
Latest member
ksonnia

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top