Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 使用excel宏验证文件下载_Vba_Excel - Fatal编程技术网

Vba 使用excel宏验证文件下载

Vba 使用excel宏验证文件下载,vba,excel,Vba,Excel,我需要从互联网上下载大量文件,因此我通过引用web编写了一个excel宏。但现在我想从网站上下载一些受密码保护的文件,那么我应该如何修改我的代码,以便它检查身份验证,并在需要时提示输入用户名和密码? 提前谢谢 这是我的vba代码 子下载文件() ”声明必要的变量。 将sh设置为工作表 Dim DownloadFolder作为字符串 最后一排一样长 Dim SpecialChar()作为字符串 Dim SpecialChars被发现为双精度 将文件路径设置为字符串 我想我会坚持多久 作为整数的Di

我需要从互联网上下载大量文件,因此我通过引用web编写了一个excel宏。但现在我想从网站上下载一些受密码保护的文件,那么我应该如何修改我的代码,以便它检查身份验证,并在需要时提示输入用户名和密码? 提前谢谢 这是我的vba代码 子下载文件()

”声明必要的变量。
将sh设置为工作表
Dim DownloadFolder作为字符串
最后一排一样长
Dim SpecialChar()作为字符串
Dim SpecialChars被发现为双精度
将文件路径设置为字符串
我想我会坚持多久
作为整数的Dim j
结果很长
暗淡无光的镜子
将用户名设置为字符串
将密码设置为字符串
将文本文件设置为整数
将CFilePath设置为字符串
将凭据作为变量
'禁用屏幕闪烁。
Application.ScreenUpdating=False
'将工作表对象设置为所需的工作表。
设置sh=图纸(“主”)
'具有不能用于命名文件的特殊字符的数组。
SpecialChar()=拆分(“\/:*?”&Chr$(34)和“<>”,“”)
'找到最后一行。
与sh
.激活
LastRow=.Cells(.Rows.Count,“C”).End(xlUp).Row
以
'检查下载文件夹是否存在。
DownloadFolder=sh.Range(“B4”)
出错时继续下一步
如果Dir(DownloadFolder,vbDirectory)=vbNullString,则
MsgBox“文件夹路径不正确!”,vbCritical,“文件夹路径错误”
sh.Range(“B4”)。选择
出口接头
如果结束
错误转到0
'检查是否至少有一个URL。
如果LastRow<8,则
MsgBox“您没有输入一个URL!”,vbCritical,“没有URL错误”
sh.Range(“C8”)。选择
出口接头
如果结束
'清除结果列。
sh.Range(“E8:E”和LastRow).ClearContents
'如果不存在,请添加反斜杠。
如果正确(DownloadFolder,1)“\”则
DownloadFolder=DownloadFolder&“\”
如果结束
'计算将不下载的文件数。
计数器错误=0
'将internet文件保存在硬盘的指定文件夹中。
出错时继续下一步
对于i=8到最后一行
'使用给定的文件名。
如果不是sh.Cells(i,4)=vbNullString,则
'获取给定的文件名。
FilePath=sh.Cells(i,4)
'检查文件路径是否包含特殊/非法字符。
对于j=LBound(SpecialChar)到UBound(SpecialChar)
SpecialCharFound=InStr(1,文件路径,SpecialChar(j),vbTextCompare)
'如果发现非法字符,请将其替换为“-”字符。
如果SpecialCharFound>0,则
FilePath=WorksheetFunction.Substitute(FilePath,SpecialChar(j),“-”)
如果结束
下一个j
'创建最终文件路径。
FilePath=下载文件夹&文件路径
'检查文件路径是否超过允许的最大字符数。
如果Len(FilePath)>255,则
sh.Cells(i,5)=“错误”
反错误=反错误+1
如果结束
其他的
'空文件名。
sh.Cells(i,5)=“错误”
反错误=反错误+1
如果结束
'如果文件路径有效,请将文件保存到所选文件夹中。
如果UCase(sh.Cells(i,5))“错误”,则
'尝试下载并保存该文件。
结果=URL下载文件(0,sh.Cells(i,3),文件路径,0,0)
'检查文件是否已成功下载并存在。
如果Result=0而不是Dir(FilePath,vbDirectory)=vbNullString,则
“成功!
sh.Cells(i,5)=“正常”
其他的
“错!
sh.Cells(i,5)=“错误”
反错误=反错误+1
如果结束
如果结束
接下来我
错误转到0
'启用屏幕。
Application.ScreenUpdating=True
'通知用户宏已成功完成或出现错误。
如果计数器错误=0,则
“成功!
如果LastRow-7=1,则
MsgBox“文件已成功下载!”,vbInformation,“完成”
其他的
MsgBox LastRow-7&“文件已成功下载!”,vbInformation,“完成”
如果结束
其他的
“错!
如果计数器错误=1,则
MsgBox“其中一个文件出错!”,vbCritical,“错误”
其他的
MsgBox“有一个错误,”&CountErrors&“files!”,vbCritical,“error”
如果结束
如果结束

End Sub

您还可以共享Sub-URLDownloadToFile(0,sh.Cells(i,3),FilePath,0,0)的代码。您可以在这里找到使用Winhttprequest首选此方法而不是登录的任何特定原因。这种方法似乎更方便登录受密码保护的网站。我不熟悉这种方法。你能给我举个例子吗?请查看以获得指导。
'Declaring the necessary variables.
Dim sh                  As Worksheet
Dim DownloadFolder      As String
Dim LastRow             As Long
Dim SpecialChar()       As String
Dim SpecialCharFound    As Double
Dim FilePath            As String
Dim i                   As Long
Dim j                   As Integer
Dim Result              As Long
Dim CountErrors         As Long
Dim Username            As String
Dim Password            As String
Dim TextFile            As Integer
Dim CFilePath           As String
Dim Credentials         As Variant

'Disable screen flickering.
Application.ScreenUpdating = False

'Set the worksheet object to the desired sheet.
Set sh = Sheets("Main")

'An array with special characters that cannot be used for naming a file.
SpecialChar() = Split("\ / : * ? " & Chr$(34) & " < > |", " ")

'Find the last row.
 With sh
    .Activate
    LastRow = .Cells(.Rows.Count, "C").End(xlUp).Row
End With

'Check if the download folder exists.
DownloadFolder = sh.Range("B4")
On Error Resume Next
If Dir(DownloadFolder, vbDirectory) = vbNullString Then
    MsgBox "The folder's path is incorrect!", vbCritical, "Folder's Path Error"
    sh.Range("B4").Select
    Exit Sub
End If
On Error GoTo 0

'Check if there is at least one URL.
If LastRow < 8 Then
    MsgBox "You did't enter a single URL!", vbCritical, "No URL Error"
    sh.Range("C8").Select
    Exit Sub
End If

'Clear the results column.
sh.Range("E8:E" & LastRow).ClearContents

'Add the backslash if doesn't exist.
If Right(DownloadFolder, 1) <> "\" Then
    DownloadFolder = DownloadFolder & "\"
End If

'Counting the number of files that will not be downloaded.
CountErrors = 0

'Save the internet files at the specified folder of your hard disk.
On Error Resume Next
For i = 8 To LastRow

    'Use the given file name.
    If Not sh.Cells(i, 4) = vbNullString Then

        'Get the given file name.
        FilePath = sh.Cells(i, 4)

        'Check if the file path contains a special/illegal character.
        For j = LBound(SpecialChar) To UBound(SpecialChar)
            SpecialCharFound = InStr(1, FilePath, SpecialChar(j), vbTextCompare)
            'If an illegal character is found substitute it with a "-" character.
            If SpecialCharFound > 0 Then
                FilePath = WorksheetFunction.Substitute(FilePath, SpecialChar(j), "-")
            End If
        Next j

        'Create the final file path.
        FilePath = DownloadFolder & FilePath

        'Check if the file path exceeds the maximum allowable characters.
        If Len(FilePath) > 255 Then
            sh.Cells(i, 5) = "ERROR"
            CountErrors = CountErrors + 1
        End If

    Else
        'Empty file name.
        sh.Cells(i, 5) = "ERROR"
        CountErrors = CountErrors + 1
    End If

    'If the file path is valid, save the file into the selected folder.
    If UCase(sh.Cells(i, 5)) <> "ERROR" Then

        'Try to download and save the file.
        Result = URLDownloadToFile(0, sh.Cells(i, 3), FilePath, 0, 0)
        'Check if the file downloaded successfully and exists.
        If Result = 0 And Not Dir(FilePath, vbDirectory) = vbNullString Then
            'Success!
            sh.Cells(i, 5) = "OK"
        Else
            'Error!
            sh.Cells(i, 5) = "ERROR"
            CountErrors = CountErrors + 1

    End If
    End If

Next i
On Error GoTo 0

'Enable the screen.
Application.ScreenUpdating = True

'Inform the user that macro finished successfully or with errors.
If CountErrors = 0 Then
    'Success!
    If LastRow - 7 = 1 Then
        MsgBox "The file was successfully downloaded!", vbInformation, "Done"
    Else
        MsgBox LastRow - 7 & " files were successfully downloaded!", vbInformation, "Done"
    End If
Else
    'Error!
    If CountErrors = 1 Then
        MsgBox "There was an error with one of the files!", vbCritical, "Error"
    Else
        MsgBox "There was an error with " & CountErrors & " files!", vbCritical, "Error"
    End If
End If