Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.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
Excel 显式在搜索IE窗口时创建不同的结果_Excel_Vba_Internet Explorer_Object_Explicit - Fatal编程技术网

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

Excel 显式在搜索IE窗口时创建不同的结果,excel,vba,internet-explorer,object,explicit,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)

我很难确定模拟测试概念和实际项目中的实现之间的行为差异。请查看以下代码:

测试版本:

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
实施版本:

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窗口的名称。实现的版本只给我空字符串。如您所见,唯一的区别是在我的实际应用程序中,我声明了显式选项,因此必须声明所有变量。字符串是错误的变量类型还是什么


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

这两个代码都不起作用。它必须是IE_count=objShell.Windows.count。第二个代码中的IE未声明。是的,ShellWindows.Item的X as参数必须是一个变量。这回答了我的问题。谢谢