我有一个从网站下载文件的VBA脚本,但是当下载PDF文件时,当我尝试将其保存到系统的本地驱动器时,我收到一个名为 的错误运行时错误'3004':写入文件失败 。我的代码如下:
Sub login()
Dim objIE As InternetExplorer
Dim uid As String
Dim pwd As String
Dim rng As Range
Dim sh As Worksheet
Dim objCollection As Object
Dim buttonCollection As Object
Dim ieElement As Object
Dim ieButton As Object
Dim WinHttpReq As Object
Dim oStream As Object
Dim MyURL As String
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
Set sh = Sheets("Indeed Resume Download")
Set rng = sh.Range("A2")
uid = rng.Value
pwd = rng.Offset(0, 1).Value
Dim j As Long
Set objIE = New InternetExplorer 'Initialize internet object
objIE.Navigate "https://secure.indeed.com/account/login?service=my&hl=en_IN&co=IN&continue=https%3A%2F%2Fwww.indeed.co.in%2F"
objIE.Visible = True
objIE.Document.all.signin_email.Value = uid
objIE.Document.all.signin_password.Value = pwd
Set ieElement = objIE.Document.getElementsByClassName("sg-btn sg-btn-primary btn-signin")(0)
ieElement.Click
For j = 2 To sh.Cells(Rows.Count, 27).End(xlUp).Row
Set objIE = New InternetExplorer
objIE.Visible = True
MyURL = sh.Range("XDA" & j).Value
objIE.Navigate MyURL
Do While objIE.Busy = True
DoEvents
Loop
Set ieButton = objIE.Document.getElementsByClassName("button download_button"(0)
ieButton.Click
WinHttpReq.Open "GET", MyURL, False
WinHttpReq.Send
MyURL = WinHttpReq.responseBody
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1
oStream.Write WinHttpReq.responseBody
oStream.SaveToFile ("D:\Downloaded Indeed Resume - Store")
oStream.Close
Next j
Set objIE = Nothing
End Sub