Download the file using VBA WebBrowser and DoFileDownload

First, start WebBrowser

Start when Excel opens: activate the table where the WebBrowser, and then activate the table is activated in the event of WebBrowser, WebBrowser navigate to the Web page.

1: Activate the worksheet in the workbook where webbrowser start

Private Sub Workbook_Open()

    ThisWorkbook.Worksheets("web").Activate

End Sub

 

2: When the worksheet is activated, set the page navigation webbrowser

Private Sub Worksheet_Activate()

    WebBrowser1.Activate

    WebBrowser1.Navigate "https://...."

End Sub

You can also activate a button, to skip.

 

Second, the use DoFileDownload

1. Call the API declaration before

Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" ( _

ByVal pCaller As Long, _

ByVal szURL As String, _

ByVal szFileName As String, _

ByVal dwReserved As Long, _

ByVal lpfnCB As Long _

) As Long

Parameters explanation:

pCaller : If the application is not a call ActiveX component, you can set to Null

szURL : URL to download

szFileName : storage file name

dwReserved : must be NULL

lpfnCB things associated with the download progress:

 

2. Create a call to the API function function, here it is mainly used to determine whether to download the file behind the success

Public Function downloadFile(ByVal strURL As String, ByVal strFile As String) As Boolean

Application.EnableCancelKey = xlDisabled

Dim lngReturn&

'With lngReturn received return result

lngReturn = URLDownloadToFile(0, strURL, strFile, 0, 0) 

' Note: U RLDownloadToFile function returns 0 indicates that the file is downloaded successfully

'Returns the results to determine whether the 0, True is returned, otherwise returns False

If lngReturn = 0 Then

    downloadFile = True

Else

    downloadFile = False

End If

End Function

 

3. Create a download of executive function

Private Function DL_rpt(ByVal Down_link As String)

    dim FileName$

    FileName = "abc.tsv"

    If downloadFile(Down_link , FileName) = True Then

        msgbox "Download Successfully"

    Else

        msgbox "Download Failed"

    End If

End Sub

PS:

  1. URLDownloadToFile default file is downloaded to the path where the current excel workbook
  2. If there are duplicate file names, it will directly cover

 

Third, the use WebBrowser enter a web page, and then call the function DL_rpt

(Because the downloaded file links may be to enter a page in order to use or see)

Sub Into_HomePage()

    Error GoTo Err_Handle ON     'went wrong Err_Handle

    Wait_for_reaction Call    'to call a custom function waiting for a response page

    If WebBrowser1.Document.ReadyState = "complete" Then

        WebBrowser1.Document.getElementById("searchAccount.searchString").innerText = 105030  '1

        WebBrowser1.Document.all("go-btn").Click    '2

        a = WebBrowser1.Document.getElementById("row").Children.tags("tbody")(0).Children.tags("tr")(1) _
            .Children.tags("td")(0).Children.tags("a")(0).innerText   '3

        WebBrowser1.Document.getElementById("row").Children.tags("tbody")(0).Children.tags("tr")(1) _
            .Children.tags("td")(0).Children.tags("a")(0).Click   '3

        'The above two steps after waiting for page response is completed, the web page using WebBrowser operation:

        1. Locate the id "searchAccount.searchString" elements, fill 105 030

        2. id as "go-btn" elements in clicks

        3. Get an id of "row", in this text id below a certain label, and then click a tag to operate, it is understood Children.tags ( "tbody") (0) .Children.tags ( "tr ") (1) means

        Call wait_for_reaction

        Msg_for_reaction_completed Call   'just to see if the page response is completed, may be omitted

        dim dl_URL$

        dl_URL = "https://......."

        Call DL_File(dl_URL)

    End If

    Exit Sub

Err_Handle:

        Now the TimeValue + Application.OnTime ( "0: 0:. 5"), "Into_HomePage"    'Wait 5 seconds, the restart process Into_HomePage

End Sub

Sub wait_for_reaction()

    'Wait time set 5 seconds, the cycle continues until a page response is complete, it DoEvent representative of VBA program continues to run in front of
    the Do the While WebBrowser1.Document.ReadyState <> "Complete" Or WebBrowser1.ReadyState <> READYSTATE_COMPLETE
        T = the Timer
        the While the Timer < . 5 + T
            the DoEvents
        Wend
    Loop
End Sub

Sub msg_for_reaction_completed()
    If WebBrowser1.Document.ReadyState = "complete" Then
        MsgBox "WebPage Loaded Finished !"
    End If
End Sub

 

Four, webbrowser another file download feature: DoFileDownload Save As

Statement API calls

Private Declare PtrSafe Function DoFileDownload Lib "shdocvw.dll" (ByVal lpszFile As String) As Long

During the call

dim Download_Url as String

dim Save_File as String

Download_Url = "Https://......"

Save_File = StrConv(Download_Url, vbUnicode)

Call DoFileDownload(Save_File)

PS : there will be pop determine whether to download a temporary solution is not found

 

There are other problems continue to improve ....

Guess you like

Origin blog.csdn.net/weixin_37855575/article/details/92384877