Using Data from Web, how to login?

2.9k Views Asked by At

I have a macro that uses the "Data from Web" function. I have logged into the website (in Internet Explorer) that I'm pulling data from - but the results I get in Excel just keep telling me I'm not logged in.

Is there a special way to login via Excel for "Data from Web"? I know it works, as I used the Macro Recorder to learn how Excel gets the data - and doing so manually, the website asked me to login in the "Excel IE Browser window"...but it's been over an hour, so I was logged out. How do I log in again to use it?

here's the applicable data pull code if it helps (the URL works fine, once logged in):

 With ActiveSheet.QueryTables.Add(Connection:="URL;" & theURL, Destination:=webInfoWS.Range("$A$2"))
        .name = cel.Value & " hex"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .WebSelectionType = xlSpecifiedTables
        .WebFormatting = xlWebFormattingNone
        .WebTables = "3"
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With
1

There are 1 best solutions below

0
On

For the mean time, I found a work around (mainly from this thread):

Adding this after Debug.Print "Opening " & theURL and just before With ActiveSheet.QueryTables.Add(...)

    ''' Log in to the web stuff
    Set ie = CreateObject("InternetExplorer.Application")
    With ie
        .Visible = True
        .Navigate theURL
        Do Until .READYSTATE = 4
            DoEvents
        Loop
        If Left(.Document.Title, 5) <> "Welcome!" Then
            .Document.all.Item("username").Value = "My User Name"
            .Document.all.Item("password").Value = "MyPassword"
            .Document.forms(0).submit
        End If
      '   .Quit
    End With
    ''''''

What this does is actually opens the IE Window, then (automated) puts in my username and password, and submits.

However, if I run the macro again (meaning that I already did log in), this gives me an error, because there is no username/password entry form.

Thoughts on how to get around - use On Error Goto Next, but I don't like using that, but it might be the best option. I think I'll try instead to get the Window title (via HTML) and check if that is the login form or not...

Edit: A note on how to know that the .Item("____") is "username" and "password". That just comes from the HTML Input ID tag:

enter image description here

You'll notice in the post I found this in, the text in the .Item() is different - I assume because that HTML ID is different as well.

EDIT 2: This doesn't work! I'm able to log in, see the web page in IE, but when I get to .Refresh BackgroundQuery:=False, the resulting information is the text saying I need to login :/