1

请尝试下面的代码,正在做的是从网页表面下载一些数据,但我需要它自动工作,并且在打开 Excel 时出现问题,弹出窗口。有什么办法可以摆脱它自动按下启用?

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 DownPDF()
' This macro downloads the pdf file from webpage
' Need to download MSXML2 and MSHTML parsers and install
Application.OnTime Now + TimeValue("00:01:10"), "DownPDF"
Dim sUrl As String
Dim xHttp As MSXML2.XMLHTTP
Dim hDoc As MSHTML.HTMLDocument
Dim hAnchor As MSHTML.HTMLAnchorElement
Dim Ret As Long
Dim sPath As String
Dim i As Long

sPath = "C:\Documents and Settings\ee28118\Desktop\Ordine\"
sUrl = "http://cetatenie.just.ro/wp-content/uploads/"

'Get the directory listing
Set xHttp = New MSXML2.XMLHTTP
xHttp.Open "GET", sUrl
xHttp.send

'Wait for the page to load
Do Until xHttp.readyState = 4
    DoEvents
Loop

'Put the page in an HTML document
Set hDoc = New MSHTML.HTMLDocument
hDoc.body.innerHTML = xHttp.responseText

'Loop through the hyperlinks on the directory listing
For i = 0 To hDoc.getElementsByTagName("a").Length - 1
    Set hAnchor = hDoc.getElementsByTagName("a").Item(i)

    'test the pathname to see if it matches your pattern
    If hAnchor.pathname Like "Ordin-*.2013.pdf" Then
        Ret = URLDownloadToFile(0, sUrl & hAnchor.pathname, sPath & hAnchor.pathname, 0, 0)

        If Ret = 0 Then
            Debug.Print sUrl & hAnchor.pathname & " downloaded to " & sPath
        Else
            Debug.Print sUrl & hAnchor.pathname & " not downloaded"
        End If
    End If
Next i

End Sub
4

2 回答 2

1

尝试在桌面上将您的 Excel 安全级别设置得非常高或非常低,那么 Exel 将无法保护您免受打开工作簿时可能运行的恶意代码的侵害(但无论如何,一个好的防病毒软件都会这样做!)

  • 非常高意味着它不会问你
  • 低意味着它不会问你。

高温高压

菲利普

于 2013-03-28T14:43:17.243 回答
1

它要求您启用宏的原因是因为宏实际上会损害您的 PC。这是一个安全问题,您不能通过 VBA 自动覆盖该功能。

一个人必须手动转到Excel:“信任中心->信任中心设置->宏设置->启用所有宏(不推荐;有潜在危险的代码可以运行)”

于 2013-03-28T15:13:48.463 回答