User Defined Type not defined

mrmmickle1

Well-known Member
Joined
May 11, 2012
Messages
2,461
I have the below code:

Which I am trying to modify to fit my needs:

Rich (BB code):
Sub Test()
    Const cURL = "Website Here" 'Enter the web address here
    Const cUsername = "XXXXXX" 'Enter your user name here
    Const cPassword = "XXXXXX" 'Enter your Password here
    
    Dim IE As InternetExplorer
    Dim doc As HTMLDocument
    Dim LoginForm As HTMLFormElement
    Dim UserNameInputBox As HTMLInputElement
    Dim PasswordInputBox As HTMLInputElement
    Dim SignInButton As HTMLInputButtonElement
    Dim HTMLelement As IHTMLElement
    Dim qt As QueryTable
        
    Set IE = New InternetExplorer
    
    IE.Visible = True
    IE.Navigate cURL
    
    'Wait for initial page to load
    
    Do While IE.ReadyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop
    
    Set doc = IE.Document
    
    'Get the only form on the page
    
    Set LoginForm = doc.forms(0)
     
    Set UserNameInputBox = LoginForm.elements("userid")
    UserNameInputBox.Value = cUsername
    
    Set PasswordInputBox = LoginForm.elements("password")
    PasswordInputBox.Value = cPassword
    
    Set SignInButton = LoginForm.elements("btnSignon")
    SignInButton.Click
            
    'Wait for the new page to load
    
    Do While IE.ReadyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop
    
    End Sub

I get the error User Defined type not defined on line:
Rich (BB code):
Dim IE As InternetExplorer

Do I need to use references? Is there a quick fix for this?

I have code that works for a particular website but I am having trouble making it work for others. This code seems more flexible than the other code I have that Norie helped me with:

Rich (BB code):
Option Explicit
 
Public Sub Press_Button()
     
     
     'make sure you add references to Microsoft Internet Controls (shdocvw.dll) and
     'Microsoft HTML object Library.
     'Code will NOT run otherwise.
     
    Dim objIE As SHDocVw.InternetExplorer 'microsoft internet controls (shdocvw.dll)
    Dim htmlDoc As MSHTML.HTMLDocument 'Microsoft HTML Object Library
    Dim htmlInput As MSHTML.HTMLInputElement
    Dim htmlColl As MSHTML.IHTMLElementCollection
     
    Set objIE = New SHDocVw.InternetExplorer
     
    With objIE
        .Navigate "https://www.wellsfargo.com/" ' Main page
        .Visible = 1
        Do While .READYSTATE <> 4: DoEvents: Loop
            Application.Wait (Now + TimeValue("0:00:02"))
             
             'set user name and password
            Set htmlDoc = .document
            Set htmlColl = htmlDoc.getElementsByTagName("INPUT")
            Do While htmlDoc.READYSTATE <> "complete": DoEvents: Loop
                For Each htmlInput In htmlColl
                    If htmlInput.Name = "userid" Then
                        htmlInput.Value = "Username Here"
                    Else
                        If htmlInput.Name = "password" Then
                            htmlInput.Value = "Password Here"
                        End If
                    End If
                Next htmlInput
                 
                 'click login
                Set htmlDoc = .document
                Set htmlColl = htmlDoc.getElementsByTagName("input")
                Do While htmlDoc.READYSTATE <> "complete": DoEvents: Loop
                    For Each htmlInput In htmlColl
                        If Trim(htmlInput.Type) = "submit" Then   ' This is the button name I need.  Submit clicks a different button that directs me to the wrong page
                            htmlDoc.forms(1).submit
                            Exit For
                        End If
                    Next htmlInput
                End With
                 
            End Sub

My end goal is to log on to several websites and pull data out of tables on the websites back into excel so that I can have a one stop shop instead of logging into many websites manually. Any Help would be appreciated.
 
Last edited:

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Take a look at the first section of the code you posted:
'make sure you add references to Microsoft Internet Controls (shdocvw.dll) and
'Microsoft HTML object Library.
'Code will NOT run otherwise.
Have you selected these two references in your new project? Without it,
Dim IE As InternetExplorer
is not a valid variable declaration.
 
Upvote 0
After referencing the HTML Object Library and Microsoft Internet Controls I now recieve"

run time error 91 object variable with block variable not set.
 
Upvote 0
It looks like you may have a problem with the structure of one of your loops. I am not familiar with the methodology you have chosen to write your loops. If you hit debug, it should tell you which one is the problem.

Did you do it yourself, or did you copy this from somewhere else?
Typically, I write my loops like this, in block format instead of trying to put it all on one line:
Code:
Do While ...
   ...
Loop
 
Upvote 0
Joe4,

As you mentioned I did not write the code. I guess I just need to take a crash course in HTML references and the basics of getting the IE browser to function through VBA. I will try to search the forum to find more reference points for this sort of thing again. I may be using the wrong search keys. Thank you for the help though. I put the loop on a separate line but it did not help. I will keep trying my hand at it until I get it to work though. I will post the solution when I have it in hand.

shg,

It does not specify the line when I run it.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

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