在搜索IE窗口时,显式创建不同的结果

时间:2014-08-11 11:19:52

标签: excel vba internet-explorer object explicit

我在确定模拟测试概念与实际项目中的实施之间的行为差​​异时遇到了一些困难。请看下面的代码:

测试版本:

Dim tempString As Variant
Dim objCollection As Object

Private Sub TryButton_Click()
marker = 0
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
For x = 0 To (IE_count - 1)
    On Error Resume Next    ' sometimes more web pages are counted than are open
    my_url = objShell.Windows(x).document.Location
    my_title = objShell.Windows(x).document.Title
    msgBox my_title
    If my_title = "Windowname" Then 'compare to find if the desired web page is already open
        Set IE = objShell.Windows(x)
        marker = 1
        Exit For
    Else
    End If
Next
End Sub

Implimented版本:

Option Explicit

Private Sub OOAButton_Click()
Dim objCollection As Object
Dim marker As Integer
Dim objShell As Object
Dim IE_count As Integer
Dim X As Integer
Dim my_url As String
Dim my_title As String

marker = 0
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
For X = 0 To (IE_count - 1)
    On Error Resume Next    ' sometimes more web pages are counted than are open
    my_url = objShell.Windows(X).document.location
    my_title = objShell.Windows(X).document.Title
    MsgBox my_title
    If my_title = "Windowname" Then 'compare to find if the desired web page is already open
        Set IE = objShell.Windows(X)
        marker = 1
        Exit For
    Else
    End If
    Next
End Sub

我的测试版本运行良好,并向我显示我打开的各种IE窗口的名称。这个版本只给我空白字符串。正如您所看到的,唯一的区别是在我的实际应用程序中,我已经声明了显式选项,因此必须声明我的所有变量。字符串是错误的变量类型还是什么?

任何帮助将不胜感激。感谢

0 个答案:

没有答案