Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.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 通过VBA获取现有IE_Excel_Vba_Internet Explorer - Fatal编程技术网

Excel 通过VBA获取现有IE

Excel 通过VBA获取现有IE,excel,vba,internet-explorer,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 Shell

我的目标是将同一站点上多个网页上的数据刮到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
我不想使用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

按照此链接查看使用打开的IE窗口的代码。

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

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

函数GetIE()作为对象 Dim ShellApp作为对象,ShellWindows作为对象,i作为长 将对象作为对象 设置ShellApp=CreateObject(“Shell.Application”) 设置ShellWindows=ShellApp.Windows() 对于i=0的ShellWindows。计数-1 如果InStr(ShellWindows.Item(i).FullName,“C:\ProgramFiles(x86)\Internet Explorer\IEXPLORE.EXE”)为0,则 设置IEObject=ShellWindows.Item(i) 退出“无需继续…”。。。。 如果结束 下一个 如果IEObject为Nothing,则设置IEObject=CreateObject(“InternetExplorer.Application”) 设置GetIE=IEObject 端函数 你可以把这个函数放在你的代码里,把它藏在某个地方,然后在你需要IE的时候,你可以这样调用它:

我找到了!谢谢,下面的工作:

               AppActivate " - Microsoft Internet Explorer provided by [my company]"
               AppActivate " - Microsoft Internet Explorer provided by [my company]"
它可以提出任何网站,无论命名方案如何。当然,它看起来很笨拙,但如果它奏效,我也不会抱怨


祝您节日快乐!

做了一些更改,现在可以使用了

         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

以上答案的压缩组合,以下是对我有用的


GetIE
函数 (无需参考。)

用法示例:
子演示()
模糊的物体
设置ie=GetIE'获取新的/现有的ie对象
即“导航”http://stackoverflow.com“,2'2=不保留历史记录
Do:DoEvents:在ie.Busy或ie.ReadyState 4'等待加载时循环
停止做你的事
别靠近我
设置ie=无任何“清理”
端接头

您收到运行时错误5?我只能在特定选项卡名称为有焦点的选项卡名称时使用
AppActivate
功能(Win 7,IE10)。否则它会失败。你看过Selenium包装器了吗?我没有在IE中使用过它,但我在Google Chrome和Firefox中使用过它,它似乎是一个更强大的解决方案,并且本机支持选项卡式浏览。可能值得花几分钟来检查一下。除了whe之外,我没有收到任何运行时错误n我尝试使用GetObject方法,并给了我ActiveX错误,因此发现Microsoft禁用了它。至于Selenium Wrapper--这听起来像是一个梦想!//删除//我希望它能完成我希望它做的事情。我会让你知道它是如何运行的。\\n删除\\不幸的是,由于公司的限制,我无法安装它。需要一段时间才能安装如果我得到许可的话,它安装了。同时,还有其他的解决方案吗?你真的在安装第三方库时有“公司限制”吗?如果是的话,他们基本上在说:“这里是一个勺子,去为摩天大楼挖掘基础。”"。你需要的是一个更强大的工具,如果他们不让你拥有,这可能是下意识的反应。提出理由,并寻求许可。他们可能会同意。至于其他建议,不,我从未尝试在IE自动化中使用标签浏览器,正是因为这似乎不可能,或者不值得努力t当其他库允许我在其他浏览器中轻松完成时:)谢谢David的帮助!我一定会尝试安装软件。至于标签浏览器,与其说是标签本身,还不如说是整个浏览器。我正在使用sendkeys来操作选项卡,所以这没有问题。我也尝试过这种方法。事实上,这是我在挖掘时第一次尝试的地方之一。不幸的是,我根本没有注册。谢谢。我尝试了函数代码,但它给了我以下错误:“无效的过程调用或参数。”对于以下行:
If InStr(ShellWindows.Item(I).FullName,“C:\Program Files(x86)\Internet Explorer\iexplore.exe”)0,这让人困惑,因为它与您之前使用的代码完全相同,只是一个不同的字符串比较。如果该语句以前没有引发错误,则该语句不应引发错误。它是。。。我检查了三倍,以确保在原始脚本中没有错误,我没有。它什么也不做。我想我将创建一个新对象(IE),然后打开收藏夹或旧会话以打开所有选项卡。不太理想,但可以。非常感谢大卫的帮助!我肯定计划得到软件集成开发环境。这听起来像是我一直在寻找的答案。我在工作中做了很多数据搜集工作,我明白了!请看以下作品中的答案:呃,我本想写我写的评论
               AppActivate " - Microsoft Internet Explorer provided by [my company]"
               AppActivate " - Microsoft Internet Explorer provided by [my company]"
         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
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