[使用vba使用Internet Explorer 11自动下载文件

问题描述 投票:0回答:1

我正在尝试使用InternetExplorer.Application下载文件,但始终会打开一个窗口,要求保存或打开文件。有没有办法避免这种情况,并将其运行并保存在后台?这是我尝试过的代码块。

Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate "https://pastebin.com/raw/terAweb"
State = 0
Do Until State = 4
DoEvents
State = ie.readyState
Loop
Dim file: file= ie.Document.Body.innerHTML
vba automation internet-explorer-11
1个回答
0
投票

使用URL Monikers API而不是尝试与InternetExplorer Application进行通信可能会更简单。这是专门为Pastebin完成的吗?据我所知,因为它并不能真正起作用。但是我猜你可以再用一个😉

Option Explicit

Private Declare PtrSafe Function URLDownloadToFileA Lib "URLMON" _
 (ByVal pcaller As Long, _
  ByVal szurl As String, _
  ByVal szFileName As String, _
  ByVal dwReserved As Long, _
  ByVal lpfnCB As Long) As LongPtr

Sub Example()
Dim Download$

    On Error GoTo ErrorHandler

    Download = URLDownloadToFileA(0, "myURL", "C:\Users\Name\Downloads\test.txt", 0, 0)

    Exit Sub

ErrorHandler:

    MsgBox Err.Number & " " & Err.Description

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.