【问题标题】:Auto Download PDF thru URLDownloadToFile通过 URLDownloadToFile 自动下载 PDF
【发布时间】:2017-12-11 20:28:37
【问题描述】:

如何在以下代码中从网站中提取 PDF 的默认名称,而不是 blah.pdf?

Option Explicit 

Private Declare 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 

Sub z() 

    Dim strSource As String 
    Dim strDest As String 
    strSource = "http://www.cran.r-project.org/doc/manuals/R-intro.pdf" 
    strDest = "c:\temp\blah.pdf" 
    URLDownloadToFile 0, strSource, strDest, 0, 0 

End Sub

【问题讨论】:

    标签: excel excel-2007 vba


    【解决方案1】:

    使用您原来下载文件的方法很好,但您实际上是如何确定路径和文件名的?你有清单吗?你需要从网站上获取吗?关于检索仪器编号,首先将引用(VBE > 工具 > 引用)设置为 Microsoft XML、vx.0 和 Microsoft HTML 对象库,然后相应地更改指定的 URL,然后尝试...

    Option Explicit
    
    Sub GetInstrumentNumber()
    
        'Set a reference (VBE > Tools > References) to the following libraries:
        '   1) Microsoft XML, v6.0 (or whatever version you have)
        '   2) Microsoft HTML Object Library
    
        Dim XMLReq As New MSXML2.XMLHTTP60
        Dim HTMLDoc As New MSHTML.HTMLDocument
        Dim sURL As String
        Dim sInstrument As String
    
        sURL = "http://www.cran.r-project.org/..." 'change the URL adddress accordingly
    
        With XMLReq
            .Open "GET", sURL, False
            .send
            Do While .readyState <> 4
                DoEvents
            Loop
        End With
    
        If XMLReq.Status <> 200 Then
            MsgBox "Error " & XMLReq.Status & ":  " & XMLReq.statusText
            Exit Sub
        End If
    
        HTMLDoc.body.innerHTML = XMLReq.responseText
    
        sInstrument = HTMLDoc.getElementById("6063270").getElementsByTagName("tr")(0).Cells(0).innerText
        sInstrument = Trim(Split(sInstrument, ":")(1))
    
        MsgBox "Instrument number:  " & sInstrument, vbInformation
    
        Set XMLReq = Nothing
        Set HTMLDoc = Nothing
    
    End Sub
    

    【讨论】:

    • 是的,它的工作..!!它采用身份证号码而不是仪器。我想要那个仪器编号作为那个 PDF 名称。
    • 这是该链接的 HTML 代码
    • 仪器:201707030027817 卷页:201707030027817
      记录:7/3/2017 2:47:41 PM
    • 好的,我已经编辑了我的帖子以包含检索仪器编号的方法。
    • 它要求定义 HTMLDoc
    猜你喜欢
    • 1970-01-01
    • 2018-02-13
    • 1970-01-01
    • 2011-11-15
    • 2019-01-23
    • 2015-06-26
    • 1970-01-01
    • 2022-11-12
    • 2019-05-16
    相关资源
    最近更新 更多