Using VBA to extract table from login website

daftcam

New Member
Joined
Sep 20, 2016
Messages
6
Hi all!

I'm fairly decent at Excel, but absolutely no idea when it comes to VBA. I'm trying to get Excel to go to a website, fill in login details, then navigate to another website, select a table, and copy and paste it into Excel. I've looked all over the net and found this little bit of code, but for some reason when it gets to "Dim clip As DataObject" it comes up with a "Compile error: User-defined type not defined" error.

I've removed the actual website / username / password as this is confidential, but I've viewed the source of the website and am confident I've defined the username & password correctly in the coding.

I thought it might have to do with the references I have enabled, as I've had to allow "Microsoft Internet Controls" and "Microsoft HTML Object Library", but still no luck. Could anyone help with what they think the problem is? Is there something simple I'm missing here?

Code:
Sub GetTable()


Dim ieApp As InternetExplorer
Dim ieDoc As Object
Dim ieTable As Object
Dim clip As DataObject


'create a new instance of ie
Set ieApp = New InternetExplorer


'you don’t need this, but it’s good for debugging
ieApp.Visible = True


'assume we’re not logged in and just go directly to the login page
ieApp.navigate "WEBSITE"
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.readyState = READYSTATE_COMPLETE: DoEvents: Loop


Set ieDoc = ieApp.document


'fill in the login form – View Source from your browser to get the control names
With ieDoc.forms(0)
.UserName.Value = "Username"
.Password.Value = "Password"
.submit
End With
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.readyState = READYSTATE_COMPLETE: DoEvents: Loop


'now that we’re in, go to the page we want
ieApp.navigate "WEBSITE2"
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.readyState = READYSTATE_COMPLETE: DoEvents: Loop


'get the table based on the table’s id
Set ieDoc = ieApp.document
Set ieTable = ieDoc.all.Item("cmstable")


'copy the tables html to the clipboard and paste to teh sheet
If Not ieTable Is Nothing Then
Set clip = New DataObject
clip.SetText "" & ieTable.outerHTML & ""
clip.PutInClipboard
Sheet1.Select
Sheet1.Range("A1").Select
Sheet1.PasteSpecial "Unicode Text"
End If


'close 'er up
ieApp.Quit
Set ieApp = Nothing


End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Update: I've managed to debug the error (I needed to add another reference to "Microsoft Forms 2.0 Object Library") so it opens IE, goes to my first website, adds my user credentials, submits them... and then just sits there!
It doesn't look like it's correctly logged in, but also doesn't say that there's an error with the credentials (e.g. incorrect username and password).

Could anyone help me understand why it wouldn't be loading the next page (as if the credentials were correct)?
 
Upvote 0
I try this code for login via IE a so get table, but dont work. I have a yelow link:
VBA Code:
Do Until ieApp.ReadyState = READYSTATE_COMPLETE
Any ideas?
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,561
Members
449,089
Latest member
Motoracer88

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