问题描述
这是我一直在打印窗口标题的一段代码.
Here is a piece of code I have been working on to print the title of a window.
Dim my_title2 as Variant
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
MsgBox ("The number of pages is: " & IE_count)
For x = 0 To (IE_count - 1)
On Error Resume Next
my_url = objShell.Windows(x).document.Location
my_title = objShell.Windows(x).document.Title
If my_title Like "F-Engine" & "*" Then
Set ie = objShell.Windows(x)
my_title2 = ie.document.Title
'my_title2 = objShell.Windows(x).document.Title
MsgBox ("The wanted title for the page should corrrespond. " & my_title2)
Exit For
Else
End If
Next
在Set ie = objShell.Windows(x)
之后,我无法打印窗口标题.
I am having trouble printing the title of the window after Set ie = objShell.Windows(x)
.
y_title2 = ie.document.title
时,MsgBox显示:
When y_title2 = ie.document.title
, the MsgBox displays:
此句子后不打印任何内容.因此,不会显示分配给"ie"的标题.
It prints nothing after this sentence. So the title assigned to "ie" is not being displayed.
如果为my_title2 = objShell.Windows(x).document.title
,则MsgBox显示:
If my_title2 = objShell.Windows(x).document.title
, the MsgBox displays:
为什么我无法使用my_title2的第一个声明打印页面标题?
Why am I not able to print the title of the page with the first declaration of my_title2?
我这样做是为了在找到标题"F-Engine"之后验证页面是否被正确拾取.为此,我正在尝试打印Internet Explorer窗口标题的值.似乎没有任何设置和通过.
I am doing this to verify if the page is being correctly picked up after a title "F-Engine" is found. To do so, I am trying to print the value of the title of the Internet Explorer window. It seems like nothing has been set and passed.
推荐答案
并非objShell.Windows中的每个对象都代表一个IE页面/选项卡-它们可能是Windows资源管理器的实例.在这种情况下,没有文档属性可供访问.
Not every object in objShell.Windows represents an IE page/tab - they might be instances of Windows Explorer. In those cases there is no document property to access.
您可以对此进行测试,而不必使用On Error Resume Next
:
You can test for this instead of using On Error Resume Next
:
Dim w As Object, myUrl, myTitle, ie
For Each w In CreateObject("Shell.Application").Windows
If w.Name = "Internet Explorer" Then
myUrl = w.document.Location
myTitle = w.document.Title
Debug.Print myUrl, myTitle
If myTitle Like "F-Engine*" Then
Set ie = w
Debug.Print "Found: " & myTitle
Exit For
End If
End If
Next w
这篇关于如何在VBA中获取网站页面的标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!