0

I have the requirement where I have to login to site through code and then download the report file by navigating to certain location. I have successfully logged in and also reached to the URL of file download but not able to automatically save it to disk. Below is my code

Imports SHDocVw
Imports mshtml
Imports System.Net

 Module Module1
Dim HTMLDoc As HTMLDocument
Dim MyBrowser As InternetExplorer
Sub Main()
    MyGmail()
End Sub
Sub MyGmail()

    Dim MyHTML_Element As IHTMLElement
    Dim MyURL As String
    On Error GoTo Err_Clear
    MyURL = "https://example.com/"
    MyBrowser = New InternetExplorer
    MyBrowser.Silent = True
    MyBrowser.Navigate(MyURL)
    MyBrowser.Visible = True
    Do
    Loop Until MyBrowser.ReadyState = tagREADYSTATE.READYSTATE_COMPLETE
    HTMLDoc = MyBrowser.Document
    HTMLDoc.all.txtUserID.Value = "xyz@example.com" 'Enter your email id here
    HTMLDoc.all.txtPassword.Value = "test123" 'Enter your password here

   For Each MyHTML_Element In HTMLDoc.getElementsByTagName("input")
        If MyHTML_Element.Type = "submit" Then MyHTML_Element.click() : Exit For
    Next

    'Navigate to reports folder
    Dim newReportURL As String
    newReportURL = "https://some_static_url_to_navigate"
    MyBrowser.Navigate(newReportURL)
    Dim i As Integer
    Dim reportURL As String
    reportURL = ""
    i = 0
    For Each MyHTML_Element In HTMLDoc.getElementsByTagName("a")
        If DirectCast(MyHTML_Element, mshtml.IHTMLAnchorElement).innerText = "Export" And i = 1 Then

            reportURL = DirectCast(MyHTML_Element, mshtml.IHTMLAnchorElement).href
        End If

        If DirectCast(MyHTML_Element, mshtml.IHTMLAnchorElement).innerText = "Export" Then
            i = i + 1
        End If


    Next

    MyBrowser.Navigate(reportURL)

    For Each MyHTML_Element In HTMLDoc.getElementsByTagName("input")
        If MyHTML_Element.Type = "submit" Then
            MyHTML_Element.click() : Exit For
        End If

    Next


    Dim xlsReportURL As String
    xlsReportURL = DirectCast(MyBrowser.Document, mshtml.IHTMLDocument).url


Err_Clear:
    If Err.Number <> 0 Then
        Err.Clear()
        Resume Next
    End If
End Sub
 End Module

The last variable xlsReportURL contains my report url which is in .xls format. How can i save this report directly to my hard disk?

Vladimirs
  • 8,232
  • 4
  • 43
  • 79
Tweety01
  • 176
  • 2
  • 17

1 Answers1

1

There is a method DownloadFile() through the webclient. You should try use that instead of the BrowserControl

See Download a file through the WebBrowser control

Community
  • 1
  • 1
KarmaEDV
  • 1,631
  • 16
  • 27
  • I tried that but as it create new browser object it redirect me to login page – Tweety01 Jul 10 '15 at 12:29
  • Try this for setting up login info: http://stackoverflow.com/questions/17183703/using-webclient-or-webrequest-to-login-to-a-website-and-access-data – raddevus Jul 10 '15 at 12:35
  • It's not just the login, the url for report is generated dynamically after login after navigating through different pages and performing click events on page. Can all this be done with WebClient?Please advise – Tweety01 Jul 10 '15 at 12:59