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
Excel VBA代码无法从网站下载和保存文件_Excel_Vba - Fatal编程技术网

Excel VBA代码无法从网站下载和保存文件

Excel VBA代码无法从网站下载和保存文件,excel,vba,Excel,Vba,我无法使用VBA代码从网站下载并保存文件。我已经为此尝试了各种方法,但仍然无法下载该文件。如果文件是从另一个方法下载的,它就会损坏。此问题仅在此网站上存在此代码适用于其他网站。我目前正在使用Excel 2016。 请查找下面的vba代码。请帮助我,让我知道我做错了什么 Option Explicit Sub teScrapping() Const TITLES As String = "Features" Dim IE As SHDocVw.InternetExplorer

我无法使用VBA代码从网站下载并保存文件。我已经为此尝试了各种方法,但仍然无法下载该文件。如果文件是从另一个方法下载的,它就会损坏。此问题仅在此网站上存在此代码适用于其他网站。我目前正在使用Excel 2016。 请查找下面的vba代码。请帮助我,让我知道我做错了什么

Option Explicit

Sub teScrapping()

    Const TITLES As String = "Features"

    Dim IE As SHDocVw.InternetExplorer
    Set IE = New InternetExplorer
    Dim TitlesCount As Long, NoChangesCount As Long
    Dim fileLink As MSHTML.IHTMLElementCollection
    Dim searchBoxValue As String
    Dim html As HTMLDocument
    Dim WinHttpReq As Object
    Dim oStream As Object
    Dim cnt As Integer
    Dim DownloadStatus As Long
    Dim LinkStpFile As String
    Dim LinkDrawingFile As String


    searchBoxValue = "5-212522-1"

    'On Error Resume Next

    IE.Visible = True

    IE.navigate "https://www.te.com/usa-en/home.html"

    While IE.readyState <> 4 Or IE.Busy
        DoEvents
    Wend

    Dim idoc As MSHTML.HTMLDocument
    Set idoc = IE.document

    idoc.getElementById("search-input").Value = searchBoxValue

    Dim doc_ele As MSHTML.IHTMLElement
    Dim doc_eles As MSHTML.IHTMLElementCollection
    Set doc_eles = idoc.getElementsByTagName("INPUT")

    For Each doc_ele In doc_eles
        If doc_ele.getAttribute("value") = "Search by part # or keyword" Then
            doc_ele.Click
            Exit For
        Else
        End If
    Next doc_ele

    ' Waiting page to load competely
    Set html = IE.document
    On Error Resume Next
    Do
        DoEvents
        Application.Wait Now() + TimeValue("00:00:02")
        TitlesCount = GetClassCount(html, TITLES)
        html.parentWindow.scrollBy 0, 99999
        If TitlesCount = GetClassCount(html, TITLES) Then
            NoChangesCount = NoChangesCount + 1
        Else
            NoChangesCount = 0
        End If
    Loop Until NoChangesCount = 5 ' If no changes for some attempts, assume end of dynamic page
    On Error GoTo 0

    LinkStpFile = "https://www.te.com/commerce/DocumentDelivery/DDEController?Action=showdoc&DocId=Customer+View+Model%7FCVM_5-212522-1%7FAE%7F3d_stp.zip%7FEnglish%7FENG_CVM_CVM_5-212522-1_AE.3d_stp.zip%7F5-212522-1"

    'Downloading stp fille
    Set WinHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")
    WinHttpReq.Open "POST", LinkStpFile, False, "username", "password"
    WinHttpReq.send

    If WinHttpReq.Status = 200 Then
        Set oStream = CreateObject("ADODB.Stream")
        oStream.Open
        oStream.Type = 1
        oStream.Write WinHttpReq.responseBody
        oStream.SaveToFile "C:\MyDownloads\StepFile.zip", 2 ' 1 = no overwrite, 2 = overwrite
        oStream.Close
    End If


    'Download Drawing File
    LinkDrawingFile = "https://www.te.com/commerce/DocumentDelivery/DDEController?Action=showdoc&DocId=Customer+Drawing%7F212522%7FW%7Fpdf%7FEnglish%7FENG_CD_212522_W.pdf%7F5-212522-1"

    Set WinHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")
    WinHttpReq.Open "POST", LinkDrawingFile, False, "username", "password"
    WinHttpReq.send

    If WinHttpReq.Status = 200 Then
        Set oStream = CreateObject("ADODB.Stream")
        oStream.Open
        oStream.Type = 1
        oStream.Write WinHttpReq.responseBody
        oStream.SaveToFile "C:\MyDownloads\DrawFile.pdf", 2 ' 1 = no overwrite, 2 = overwrite
        oStream.Close
    End If


  Set IE = Nothing

End Sub

Private Function GetClassCount(Doc As HTMLDocument, ClassName As String) As Long
    GetClassCount = Doc.getElementsByClassName(ClassName).Length
End Function
选项显式
副特斯拉普语()
常量标题为String=“Features”
Dim IE作为SHDocVw.InternetExplorer
Set IE=新的InternetExplorer
暗淡的标题算长,不变的标题算长
将文件链接设置为MSHTML.IHTMLElementCollection
将searchBoxValue设置为字符串
将html设置为HTMLDocument
Dim WinHttpReq作为对象
作为对象的暗淡光束
作为整数的Dim cnt
将下载状态变暗为长
Dim LinkStpFile作为字符串
Dim LinkDrawingFile作为字符串
searchBoxValue=“5-212522-1”
'出现错误时,请继续下一步
可见=真实
即“导航”https://www.te.com/usa-en/home.html"
当IE.readyState 4或IE.Busy时
多芬特
温德
Dim idoc作为MSHTML.HTMLDocument
设置idoc=IE.document
idoc.getElementById(“搜索输入”).Value=searchBoxValue
Dim doc_ele作为MSHTML.IHTMLElement
Dim doc_eles为MSHTML.IHTMLElementCollection
Set doc_eles=idoc.getElementsByTagName(“输入”)
对于文档中的每个文档元素
如果doc_ele.getAttribute(“value”)=“按部分或关键字搜索”,则
单击“文档”
退出
其他的
如果结束
下一个doc_ele
'正在等待页面完全加载
设置html=IE.document
出错时继续下一步
做
多芬特
Application.Wait Now()+时间值(“00:00:02”)
TitlesCount=GetClassCount(html,标题)
html.parentWindow.scrollBy 0,99999
如果TitlesCount=GetClassCount(html,标题),则
NOCHANGESCONT=NOCHANGESCONT+1
其他的
NoChangesCount=0
如果结束
循环直到NoChangesCount=5'如果某些尝试没有更改,则假定动态页面结束
错误转到0
链接文件=”https://www.te.com/commerce/DocumentDelivery/DDEController?Action=showdoc&DocId=Customer+视图+模型%7FCVM_5-212522-1%7FAE%7F3d_stp.zip%7FEnglish%7FENG_CVM_CVM_5-212522-1_AE.3d_stp.zip%7F5-212522-1“
'下载stp fille
设置WinHttpRequest=CreateObject(“WinHttp.WinHttpRequest.5.1”)
WinHttpReq.Open“POST”,LinkStpFile,False,“username”,“password”
WinHttpReq.send
如果WinHttpReq.Status=200,则
设置oStream=CreateObject(“ADODB.Stream”)
奥斯特雷姆,开门
oStream.Type=1
oStream.Write WinHttpReq.responseBody
oStream.SaveToFile“C:\MyDownloads\StepFile.zip”,2'1=不覆盖,2=覆盖
奥斯特雷姆,完毕
如果结束
'下载图形文件
LinkDrawingFile=”https://www.te.com/commerce/DocumentDelivery/DDEController?Action=showdoc&DocId=Customer+图纸%7F212522%7FW%7Fpdf%7FEnglish%7Fengu CD_212522_W.pdf%7F5-212522-1“
设置WinHttpRequest=CreateObject(“WinHttp.WinHttpRequest.5.1”)
WinHttpReq.Open“POST”,LinkDrawingFile,False,“username”,“password”
WinHttpReq.send
如果WinHttpReq.Status=200,则
设置oStream=CreateObject(“ADODB.Stream”)
奥斯特雷姆,开门
oStream.Type=1
oStream.Write WinHttpReq.responseBody
oStream.SaveToFile“C:\MyDownloads\DrawFile.pdf”,2'1=不覆盖,2=覆盖
奥斯特雷姆,完毕
如果结束
设置IE=无
端接头
私有函数GetClassCount(文档作为HTMLDocument,类名作为字符串)的长度
GetClassCount=Doc.getElementsByClassName(ClassName).Length
端函数

您的代码(WinHTTP部分)适用于我,但对于大量下载,您可以使用。WinHTTP部分也适用于其他网站,但问题仅出现在给定网站上。我试图下载的文件不是一个很大的文件,但不知道为什么这个网站会出现这个问题。