首页 > 解决方案 > 如何使用 VB.NET 区分 SHDocVw.InternetExplorer 和 SHDocVw.WebBrowser_v1?

问题描述

我有一个 WPF 应用程序,它具有循环浏览打开的 IE 网页(是的,我们仍在使用 IE)并从 HTML 中抓取数据的功能。有时,用户会收到一条错误消息,指出“未找到类型 WebBrowser_V1 上的公共成员‘名称’”,然后是“无法将‘System._ComObject’类型的 COM 对象转换为接口类型‘mshtml.HTMLDocument’......”我不是找到太多关于为什么 IE 窗口偶尔会被视为“WebBrowser_V1”而不是“Internet Explorer”的答案。目前,我们找到的唯一解决方案是关闭所有 IE 实例并重新启动应用程序,如果不起作用,则重新启动 PC。我一直无法在调试中复制错误。任何人都可以协助提供有关我如何解决此问题的见解吗?

    Public Function loadKeyIDs() As DataTable
    Dim dt As New DataTable
    dt.Columns.Add("keyid")
    dt.Columns.Add("clientAcct")
    dt.Columns.Add("clientName")
    dt.Columns.Add("legalEntity")
    dt.Columns.Add("vendor")
    Dim shellWins As New SHDocVw.ShellWindows
    Dim explorer As SHDocVw.InternetExplorer
    Try
        For Each explorer In shellWins
            If isEformTracking(explorer) Then
                Dim x As HTMLDocument = explorer.Document
                Dim cm As HTMLInputElement = x.getElementsByName("grkeyid")(0)
                If Not cm Is Nothing Then
                    Dim y As eFormValues = collectClientInfoAbbreviated(x)
                    dt.Rows.Add(y.keyid, y.clientAcct, y.clientName, y.legalEntity, y.vendor)
                End If
            End If
        Next
    Catch ex As Exception
        Dim st As New StackTrace(True)
        st = New StackTrace(ex, True)
        Dim msg As String = ex.Message + " {Stack trace: " + st.GetFrame(0).GetFileLineNumber().ToString + "}"
        MsgBox("An error has occurred when trying to collect open eForm Tracking pages. Please relay this information to support: " & msg)
    Finally
        shellWins = Nothing
        explorer = Nothing
    End Try
    Return dt
End Function

Public Function isEformTracking(ie As SHDocVw.InternetExplorer) As Boolean
    Dim ret As Boolean = False
    If ie.Application.name <> "Internet Explorer" Then
        ret = False
        GoTo ExitSub
    End If

    If ie.ReadyState <> 4 Then
        ret = False
        GoTo ExitSub
    End If

    If Not ie.LocationURL Like "*eforms*" And Not ie.LocationURL Like "*qdcws0726*" And Not ie.LocationURL Like "*qdcws0738*" Then
        ret = False
        GoTo ExitSub
    End If

    If ie.LocationURL Like "*eforms*" Or ie.LocationURL Like "*qdcws0726*" And Not ie.LocationURL Like "*qdcws0738*" Then
        If ie.LocationURL Like "*sales*" Then
            ret = False
            GoTo ExitSub
        End If
    End If

    Dim doc As HTMLDocument = ie.Document
    Dim keyIDelem As HTMLInputElement = doc.getElementsByName("grkeyid")(0)

    If keyIDelem Is Nothing Then
        ret = False
        GoTo ExitSub
    End If

    If Len(keyIDelem.value) > 0 Then
        ret = True
        GoTo ExitSub
    End If
ExitSub:
    Return ret

End Function

在此处输入图像描述

在此处输入图像描述

标签: wpfvb.netinternet-explorershdocvw.internetexplorer

解决方案


推荐阅读