Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Internet explorer 使用VBA通过窗口标题获取IE窗口对象_Internet Explorer_Vba_Automation - Fatal编程技术网

Internet explorer 使用VBA通过窗口标题获取IE窗口对象

Internet explorer 使用VBA通过窗口标题获取IE窗口对象,internet-explorer,vba,automation,Internet Explorer,Vba,Automation,我发现@mkingston提供了这个解决方案: ……但这对我不起作用。我已经添加了两个有问题的参考库,但是当我运行脚本时,我遇到了以下问题: 编译错误:由于pauseUntilIEReady而未定义Sub(由于此Sub未包含在答案中,因此我将其从脚本中删除) 编译错误:由于oGetIEWindowFromTitle,参数不是可选的(因此我尝试用逗号将其隔开以获取要编译的脚本) 脚本最终编译后,会出现以下错误: 自动化误差 系统找不到指定的文件 在这一行代码中: 对于objShellWindow

我发现@mkingston提供了这个解决方案:

……但这对我不起作用。我已经添加了两个有问题的参考库,但是当我运行脚本时,我遇到了以下问题:

编译错误:由于pauseUntilIEReady而未定义Sub(由于此Sub未包含在答案中,因此我将其从脚本中删除)

编译错误:由于oGetIEWindowFromTitle,参数不是可选的(因此我尝试用逗号将其隔开以获取要编译的脚本)

脚本最终编译后,会出现以下错误:

自动化误差 系统找不到指定的文件

在这一行代码中: 对于objShellWindows中的每个oGetIEWindowFromTitle

下面是我试图运行的代码:

Function oGetIEWindowFromTitle(sTitle As String, _
                           Optional bCaseSensitive As Boolean = False, _
                           Optional bExact As Boolean = False) As SHDocVw.InternetExplorer

Dim objShellWindows As New SHDocVw.ShellWindows
Dim found As Boolean
Dim startTime As Single

found = False
'Loop through shell windows
For Each oGetIEWindowFromTitle In objShellWindows
    found = oGetIEWindowFromTitleHandler(oGetIEWindowFromTitle, sTitle, bCaseSensitive, bExact)
    If found Then Exit For
Next

'Check whether a window was found
If Not found Then
    Set oGetIEWindowFromTitle = Nothing
Else
    'COMMENTED OUT TO GET SCRIPT TO COMPILE pauseUntilIEReady oGetIEWindowFromTitle
End If

End Function


Private Function oGetIEWindowFromTitleHandler(win As SHDocVw.InternetExplorer, _
                                  sTitle As String, _
                                  bCaseSensitive As Boolean, _
                                  bExact As Boolean) As Boolean

oGetIEWindowFromTitleHandler = False

On Error GoTo handler
'If the document is of type HTMLDocument, it is an IE window
If TypeName(win.Document) = "HTMLDocument" Then
    'Check whether the title contains the passed title
    If bExact Then
        If (win.Document.title = sTitle) Or ((Not bCaseSensitive) And (LCase(sTitle) = LCase(win.Document.title))) Then oGetIEWindowFromTitleHandler = True
    Else
        If InStr(1, win.Document.title, sTitle) Or ((Not bCaseSensitive) And (InStr(1, LCase(win.Document.title), LCase(sTitle), vbTextCompare) <> 0)) Then oGetIEWindowFromTitleHandler = True
    End If
End If
handler:
'We assume here that if an error is raised it's because
'the window is not of the correct type. Therefore we
'simply ignore it and carry on.

End Function
这对我有用

Function IEWindowFromTitle(sTitle As String) As SHDocVw.InternetExplorer

    Dim objShellWindows As New SHDocVw.ShellWindows
    Dim win As Object, rv As SHDocVw.InternetExplorer

    For Each win In objShellWindows
        If TypeName(win.Document) = "HTMLDocument" Then
            If UCase(win.Document.Title) = UCase(sTitle) Then
                Set rv = win
                Exit For
            End If
        End If
    Next

    Set IEWindowFromTitle = rv

End Function

Sub Tester()

    Dim w As SHDocVw.InternetExplorer
    Set w = IEWindowFromTitle("Google")
    If Not w Is Nothing Then
        Debug.Print w.Document.Title
    Else
        Debug.Print "Not found"
    End If

End Sub

发布您试图使用的实际代码。嗨,Tim,我最初没有发布代码,因为它与链接解决方案中建议的相同。我复制了下面的一行注释,我必须注释才能编译代码。谢谢你的回复。当我运行时,我得到:“自动化错误/系统找不到指定的文件”,VBA在这一行中断:“对于objShellWindows中的每一次胜利”对我来说很好。Win7上的32位Excel 64位。尝试在两台运行Win7的不同计算机上安装Excel 2010。除了Microsoft Internet控件和Microsoft HTML对象库之外,我还需要哪些参考库?好的,我修改了脚本,在objShellWindows/MsgBox(win.Name)/Next中为每个win包含
,返回“Windows Internet Explorer”(并使用LocationURL返回url)但是win.Document或win.Document.Name失败,因为它说文档不是有效的属性或方法。您需要引用Microsoft Shell控件和自动化
Function IEWindowFromTitle(sTitle As String) As SHDocVw.InternetExplorer

    Dim objShellWindows As New SHDocVw.ShellWindows
    Dim win As Object, rv As SHDocVw.InternetExplorer

    For Each win In objShellWindows
        If TypeName(win.Document) = "HTMLDocument" Then
            If UCase(win.Document.Title) = UCase(sTitle) Then
                Set rv = win
                Exit For
            End If
        End If
    Next

    Set IEWindowFromTitle = rv

End Function

Sub Tester()

    Dim w As SHDocVw.InternetExplorer
    Set w = IEWindowFromTitle("Google")
    If Not w Is Nothing Then
        Debug.Print w.Document.Title
    Else
        Debug.Print "Not found"
    End If

End Sub