VBA如何抓取html标题,internet-explorer – 使用VBA按窗口标题获取IE窗口对象

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

How to intercept and manipulate a Internet Explorer popup with VBA

……但它不适合我.我已经添加了两个有问题的参考库,但是当我运行脚本时,我遇到了以下问题:

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

编译错误:由于oGetIEWindowFromTitle,因此参数不是可选的(所以我尝试了这个以便编译脚本)

脚本最终编译后,它会收到此错误:

自动化错误

该系统找不到指定的文件.

在这行代码上:

对于每个oGetIEWindowFromTitle在objShellWindows中

这是我正在尝试运行的代码:

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

Sub test()

Dim ie As SHDocVw.InternetExplorer

Dim doc As HTMLDocument 'If you have a reference to the HTML Object Library

'Dim doc as Object 'If you do not have a reference to the HTML Object Library

' Change the title here as required

Set ie = oGetIEWindowFromTitle("My popup window")

Set doc = ie.Document

Debug.Print doc.getElementsByTagName("body").Item(0).innerText

End Sub

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值