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?
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