通过VBA获取现有的IE

时间:2014-09-17 18:36:41

标签: excel vba internet-explorer

我的目标是将同一网站上多个网页的数据从Excel中删除。我在IE8中的选项卡中打开了这些页面。我没有其他IE窗口打开。

我尝试过以下操作来打开IE:

AppActivate "Microsoft Internet Explorer provided by [my company]"
' It loses focus, the titlebar flashes for a fraction of a second 
' .. another code ..
Dim ShellApp
Set ShellApp = CreateObject("Shell.Application")
Dim ShellWindows
Set ShellWindows = ShellApp.Windows()
Dim i
For i = 0 To ShellWindows.Count - 1
    If InStr(ShellWindows.Item(i).FullName, "iexplore.exe") <> 0 Then
        Set IEObject = ShellWindows.Item(i) 
    End If
Next
' Did absolutely nothing, grabbed this code from another solution on stackoverflow

我也试过GetObject(我不想使用CreateObject方法),如下所示

dim ie As InternetExplorer 'also tried As Object and other variation
set ie = GetObject(, "InternetExplorer.Application")
'However this is not workable due to security risks and Microsoft 
' disabled GetObject for IE by design. 

我不想使用CreateObject或任何变体的原因是因为我已经准备好打开标签了。 AppActivate适用于Microsoft Excel,但不适用于IE。我不能做如下的确切标题栏:

AppActivate "Website name - name page - Microsoft Internet Explorer provided by [my company]"

我之所以无法使用此原因是因为带有不同命名页面的标签,不断更改标题。我也尝试过:

AppActivate InternetExplorer.Application

AppActivate InternetExplorer

AppActivate " - Microsoft Internet Explorer"

AppActivate "Microsoft Internet Explorer"

上述所有内容要么不被识别,要么仅在几分之一秒内闪烁(如第一段代码所述)。我尝试过其他方法,但是当我想使用已打开的多标签的现有IE时,它们会创建一个新的IE实例。

使用不同的代码:

AppActivate "[name of page] - Microsoft Internet Explorer provided by [my company]"

工作直到我改变它以尝试引用IE,无论我在哪个页面。然后旧的AppActivate代码将不再起作用。我打破了它。没有备份我是愚蠢的。

我正在运行的系统:

Windows 7,IE8(我正在等待我的公司升级),Excel 2010

5 个答案:

答案 0 :(得分:2)

进行了一些更改,现在可以正常使用

         Function GetIE() As Object

            Dim ShellApp As Object, ShellWindows As Object
            Dim IEObject As Object

            Set ShellApp = CreateObject("Shell.Application")

            Set ShellWindows = ShellApp.Windows()

            Dim item As Object
            On Error GoTo 0
            Dim sName As String

               For Each ObjWind In ShellWindows
                'On Error Resume Next
                 If (Not ObjWind Is Nothing) Then
                    sName = ObjWind.Name
                     If sName = "Internet Explorer" Then
                        Set IEObject = ObjWind
                        Exit For  'No need to continue....
                     End If
                 End If

                Next

            If IEObject Is Nothing Then Set IEObject = CreateObject("InternetExplorer.Application")

            Set ShellApp = Nothing

            Set GetIE = IEObject

            End Function

答案 1 :(得分:2)

以上答案的压缩组合,这对我有效。


GetIE功能

(无需引用。)

Function GetIE() As Object
'return an object for the open Internet Explorer window, or create new one
  For Each GetIE In CreateObject("Shell.Application").Windows() 'Loop to find
    If (Not GetIE Is Nothing) And GetIE.Name = "Internet Explorer" Then Exit For 'Found!
  Next GetIE
  If GetIE Is Nothing Then Set GetIE=CreateObject("InternetExplorer.Application") 'Create
  GetIE.Visible = True 'Make IE window visible
End Function

用法示例:

Sub demo()
    Dim ie As Object
    Set ie = GetIE                                        'get new/existing IE object
    ie.Navigate "http://stackoverflow.com", 2             '2=don't keep history
    Do: DoEvents: Loop While ie.Busy or ie.ReadyState <> 4'wait til loaded
    Stop                                                  'do your stuff
    ie.Quit                                               'close IE
    Set ie = Nothing                                      'clean up
End Sub

答案 2 :(得分:0)

答案 3 :(得分:0)

好的,所以我注意到你尝试使用Shell对象的第一个方法可能存在问题。

.FullName属性的值可能不正确。 (当我调试并检查ShellWindows集合时,我看到路径""C:\Program Files (x86)\Internet Explorer\"

Function GetIE() As Object

Dim ShellApp as Object, ShellWindows as Object, i as Long 
Dim IEObject As Object

Set ShellApp = CreateObject("Shell.Application")
Set ShellWindows = ShellApp.Windows()

For i = 0 To ShellWindows.Count - 1
    If InStr(ShellWindows.Item(i).FullName, "C:\Program Files (x86)\Internet Explorer\IEXPLORE.EXE") <> 0 Then
       Set IEObject = ShellWindows.Item(i)
       Exit For  'No need to continue....
    End If
Next

If IEObject Is Nothing Then Set IEObject = CreateObject("InternetExplorer.Application")

Set GetIE = IEObject

End Function

你可以把这个功能放在你的代码中,只需把它塞进某个地方,然后每当你需要IE时你就可以这样称呼它:

答案 4 :(得分:0)

我明白了!感谢to this thread,以下工作:

               AppActivate " - Microsoft Internet Explorer provided by [my company]"
               AppActivate " - Microsoft Internet Explorer provided by [my company]"

无论命名方案如何,它都可以提出任何网站。当然,它看起来很笨拙,但如果有效,我就不会抱怨。

快乐的一天!