Myriad_Rocker
Questioning Reality
- Local time
- Today, 05:37
- Joined
- Mar 26, 2004
- Messages
- 166
Hey folks, have a question that I'm getting stumped on. I'm trying to log in to a retailer's website to download some data so I can report off the data. Thing is, I want to download this automatically without having to log in and download the file so I can then automatically load the data and have the reports ready when I come into work in the morning.
I have it so that it opens up IE and logs in just fine. It clicks on the appropriate link for the "2010 Data Files" and then I search for the link to the data file that I need. From there, I don't know what to do. I am currently trying to use a SendKey to right click but after that, I can't access the Save As or anything.
Is there a way that I don't even have to use a SendKey to right click on the link to save?
I've tried accessing the link directly but it requires me to log in and use the actual website to download the file. Otherwise, I just get an empty file.
Here's some of my code with the username and password taken out.
I have it so that it opens up IE and logs in just fine. It clicks on the appropriate link for the "2010 Data Files" and then I search for the link to the data file that I need. From there, I don't know what to do. I am currently trying to use a SendKey to right click but after that, I can't access the Save As or anything.
Is there a way that I don't even have to use a SendKey to right click on the link to save?
I've tried accessing the link directly but it requires me to log in and use the actual website to download the file. Otherwise, I just get an empty file.
Here's some of my code with the username and password taken out.
Code:
Option Explicit
Public Enum IE_READYSTATE
Uninitialised = 0
Loading = 1
Loaded = 2
Interactive = 3
complete = 4
End Enum
Private Sub Workbook_Open()
Const URL = "https://www.loblawslink.com/sites/CanadaDryMotts"
Const UserID = "******"
Const Pwd = "******"
Dim ie As Object
Dim doc As HTMLDocument
Dim PageForm As HTMLFormElement
Dim UserID_TextBox As HTMLInputElement
Dim Password_TextBox As HTMLInputElement
Dim FormButton As HTMLInputButtonElement
Dim Elem As IHTMLElement
Dim LinkElement As IHTMLElementCollection
Dim Link As HTMLAnchorElement
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate URL
'Wait for page to load
Do While ie.busy Or Not ie.readyState = IE_READYSTATE.complete: DoEvents: Loop
Set doc = ie.document
Set PageForm = doc.forms(0)
Set UserID_TextBox = PageForm.elements("user_name")
Set Password_TextBox = PageForm.elements("password")
UserID_TextBox.Value = UserID
Password_TextBox.Value = Pwd
PageForm.submit
'Wait for page to load
Do While ie.busy Or Not ie.readyState = IE_READYSTATE.complete: DoEvents: Loop
Set PageForm = Nothing
'Code here to get the files
Set PageForm = doc.forms(0)
Set LinkElement = PageForm.getElementsByTagName("a")
For Each Link In LinkElement
If Link.innerHTML = "2010 Data Files" Then
Link.Click
Exit For
End If
Next Link
'Wait for page to load
Do While ie.busy Or Not ie.readyState = IE_READYSTATE.complete: DoEvents: Loop
Set PageForm = Nothing
Set LinkElement = Nothing
Set PageForm = doc.forms(0)
Set LinkElement = PageForm.getElementsByTagName("a") '<--sometimes it errors here for some reason
For Each Link In LinkElement
If Link.innerHTML = "Current Loblaw Store Listing" Then
Link.setActive
SendKeys ("+{F10}")
Exit For
End If
Next Link
'Wait for page to load
Do While ie.busy Or Not ie.readyState = IE_READYSTATE.complete: DoEvents: Loop
Set PageForm = Nothing
Set LinkElement = Nothing
'ie.Quit
End Sub