通过VBA实现IE的自动化按预期执行,但会引发自动化错误

时间:2015-04-20 08:21:52

标签: internet-explorer excel-vba vba excel

我有以下代码,应该打开Internet Explorer以下载文件。

Sub hentRapport()
  Dim IEapp As Object
  Dim WebUrl As String

  Set IEapp = CreateObject("InternetExplorer.Application") 'Set IEapp = InternetExplorer
  WebUrl = Oversikt.Range("Adresse")

  With IEapp
    .Silent = True 'No Pop-ups
    .Visible = True 'Set InternetExplorer to Visible
    .Navigate WebUrl 'Load web page

    'Run and Wait, if you intend on passing variables at a later stage
    Do While .Busy
        DoEvents
    Loop

    Do While .ReadyState <> 4
        DoEvents
    Loop
  End With
End Sub

Internet Explorer(IE 11.0.9600.17691)按预期打开,I get up the dialog for downloading the file,但同时我从宏中收到错误:

enter image description here

错误发生在

Do While .ReadyState <> 4

我无法弄清楚原因。该行不简单地表明Excel在接受其他输入之前不需要等待IE执行其操作吗?


找到解决方案

经过大量的谷歌搜索后,我终于遇到了this page,其中包含一个解决方案:

  

该问题与IE8和保护模式:开启功能有关。这样做的目的是防止恶意软件运行,但它也阻止合法的VBA代码运行。根据您的工作环境,禁用此功能可能不是一种选择。更不用说与此相关的一般风险。

     

我推荐以下解决方案。

     

而不是使用:

     

Set ie = CreateObject("InternetExplorer.Application&qu ot;)

     

使用:

     

Set ie = New InternetExplorerMedium

     

您需要添加对Microsoft Internet Controls的引用。

现在我希望我的所有用户都拥有该库:D

2 个答案:

答案 0 :(得分:1)

更快的事情怎么样?使用我认为IE正在使用的API函数,以便下载文件。

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


Public Function DownFromWeb(strURL As String, strFile As String) As Boolean
    Dim ret As Long

    ret = URLDownloadToFile(0, strURL, strFile, 0, 0)

    If ret Then
        MsgBox "Failed to download file", vbExclamation
    End If

    DownFromWeb = (ret = 0)
End Function

Public Sub TestDownload()
    Const URL As String = _
        "https://www.gravatar.com/avatar/eab3ce1e413f1043da3a1574a0ab7360?s=24&d=identicon&r=PG&f=1"

    Dim sDstFolder As String
    Dim sFullPathFileName As String

    ' get the destination of the temp folder.
    ' it should give you the path like you do in windows:
    ' Start-> Run-> %temp%
    sDstFolder = Environ("temp")

    ' add a "\" at the end of the destination path as needed
    sDstFolder = sDstFolder & IIf(Right(sDstFolder, 1) = "\", "", "\")

    ' combine the path with a file name to your choosing
    sFullPathFileName = sDstFolder & "360.png"

    ' This will try to delete the previously downloaded file( just in case)
    On Error Resume Next
    Kill sFullPathFileName
    On Error GoTo 0

    If (DownFromWeb(URL, sFullPathFileName) = True) Then
          ' Open the file (probably a windows popup will appear):
          Shell "explorer " & sFullPathFileName
          ' OR
          ' if it is an excel file, you could do:
          ' Workbooks.Open(sFullPathFileName).Activate
    End If
End Sub

!!!顺便说一句,如果你只需要下载并打开一个excel文件, office有一个内置的选项(也可以用word中的doc文件)。在这种情况下,你可能会像这样一个班轮“逃脱”:

Workbooks.Open("http://highlycited.com/highly_cited_2001.xlsx").Activate

答案 1 :(得分:0)

Excel无法识别指定的状态。如果你改变预期的回报,它应该运行正常。

While .document.ReadyState <> "complete"
  'loop
Wend

查看更多Here