自动关闭警报/弹出窗口VBA

时间:2013-03-28 14:39:39

标签: vba excel-vba popup excel

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

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

2 个答案:

答案 0 :(得分:1)

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

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

HTH

菲利普

答案 1 :(得分:1)

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

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