I'm writing a VBA script that will open internet explorer, navigate to a certain webpage, apply search filters, submit the search form, and click the "Export to Excel" button for the table of data that gets presented. The code is working perfect up to the point of clicking the button to export to Excel. I think it's a problem with security since it's on my companies Intranet and opens as a read only after selecting to "Open" the file in the pop-up.
Below is my code I am using
Sub Test_Extract()
Dim IE As Object
Dim Log_ID As Object
Dim Status As Object
Dim From_Date As Object
Dim To_Date As Object
Dim Element As Object
Dim btnInput As Object
Dim ElementCol As Object
Application.ScreenUpdating = False
With CreateObject("Shell.Application").Windows
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "WEBSITE"
End With
' loop until the page finishes loading
Do While IE.Busy
Loop
' enter username and password in textboxes
Set Log_ID = IE.document.getElementsByName("txtID")
If Not Log_ID Is Nothing Then
' fill in log id
Log_ID(0).Value = "9999"
End If
Set Status = IE.document.getElementsByName("drpStatus")
If Not Status Is Nothing Then
' fill in status field
Status(0).Value = "Both"
End If
Set From_Date = IE.document.getElementsByName("cal1")
If Not From_Date Is Nothing Then
' fill in start date
From_Date(0).Value = "01/01/2014"
End If
Set To_Date = IE.document.getElementsByName("cal2")
If Not To_Date Is Nothing Then
' fill in end date
To_Date(0).Value = "04/01/2014"
End If
' Submit the search form
With IE
.document.forms(0).submit
End With
' Click the export button
Set ElementCol = IE.document.getElementsByTagName("btnExcel")
For Each btnInput In ElementCol
If btnInput.Value = "btnExcel" Then
btnInput.Click
Exit For
End If
Next btnInput
Application.ScreenUpdating = True
End Sub
Below is my code I am using
Sub Test_Extract()
Dim IE As Object
Dim Log_ID As Object
Dim Status As Object
Dim From_Date As Object
Dim To_Date As Object
Dim Element As Object
Dim btnInput As Object
Dim ElementCol As Object
Application.ScreenUpdating = False
With CreateObject("Shell.Application").Windows
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "WEBSITE"
End With
' loop until the page finishes loading
Do While IE.Busy
Loop
' enter username and password in textboxes
Set Log_ID = IE.document.getElementsByName("txtID")
If Not Log_ID Is Nothing Then
' fill in log id
Log_ID(0).Value = "9999"
End If
Set Status = IE.document.getElementsByName("drpStatus")
If Not Status Is Nothing Then
' fill in status field
Status(0).Value = "Both"
End If
Set From_Date = IE.document.getElementsByName("cal1")
If Not From_Date Is Nothing Then
' fill in start date
From_Date(0).Value = "01/01/2014"
End If
Set To_Date = IE.document.getElementsByName("cal2")
If Not To_Date Is Nothing Then
' fill in end date
To_Date(0).Value = "04/01/2014"
End If
' Submit the search form
With IE
.document.forms(0).submit
End With
' Click the export button
Set ElementCol = IE.document.getElementsByTagName("btnExcel")
For Each btnInput In ElementCol
If btnInput.Value = "btnExcel" Then
btnInput.Click
Exit For
End If
Next btnInput
Application.ScreenUpdating = True
End Sub