首页 > 解决方案 > VBA 运行时错误 13:使用 HTMLDocument 作为参数调用函数时类型不匹配

问题描述

下面的代码在一台机器上运行良好,并在使用两个参数调用函数时在第二台机器上返回错误(使用一个参数调用时,它运行良好)。该功能在另一个模块中。有人可以给我一个建议吗?

两台机器都运行 Excel 2016 和 IE11。

Dim ie As InternetExplorer
Dim htmlDoc As MSHTML.HTMLDocument
Set ie = New InternetExplorerMedium
With ie
    .Silent = True
    .navigate serverURL 'URL as string
    .Visible = False
End With

Set htmlDoc = ie.document

Call WaitForLoad(ie) 'this works fine

For Each htmlElement In htmlDoc.getElementsByTagName("input")
   If htmlElement.Type = "submit" Then htmlElement.Click: Exit For
Next

Call WaitForLoad(ie, htmlDoc) 'this returns Run-time error 13: Type mismatch

功能:

Public Function WaitForLoad(browser As InternetExplorer, _
                        Optional ByVal doc1 As MSHTML.HTMLDocument, _
                        Optional ByVal doc2 As MSHTML.HTMLDocument) _
                        As Variant

If Not browser Is Nothing Then
    Call LoadTimeoutSettings
    Do
        DoEvents
    Loop Until (browser.readyState = READYSTATE_COMPLETE) Or (Now > StopTime)
End If

If Not doc1 Is Nothing Then
    Call LoadTimeoutSettings
    Do
        DoEvents
    Loop Until (doc1.readyState = "complete") Or (Now > StopTime)
End If

If Not doc2 Is Nothing Then
    Call LoadTimeoutSettings
    Do
        DoEvents
    Loop Until (doc2.readyState = "complete") Or (Now > StopTime)
End If
End function


Function LoadTimeoutSettings()
StartTime = Now
StopTime = StartTime + TimeValue(TimeOut) 'TimeOut="00:00:15"
End Function

标签: exceltype-mismatchmshtmlvba

解决方案


推荐阅读