【问题标题】:VBA: Copy data/text from an existing open Internet Explorer tabVBA:从现有打开的 Internet Explorer 选项卡复制数据/文本
【发布时间】:2018-08-01 14:39:23
【问题描述】:

我正在尝试编写一个 VBA 代码,它允许我通过发送 CTRL+ACTRL+C 来从现有打开的 IE 选项卡复制数据SendKey 方法,然后将其粘贴到 Excel 上的工作表中。 我尝试过“从 Web 获取数据”,但这不起作用,因为我需要输入用户名/密码并且无法设置 getElementbyId 行。 所以我找到了一些代码,它会扫描已经处于活动状态的 Internet Explorer 会话的打开选项卡,但我想让 VBA 切换到匹配的选项卡并从该网站复制文本。

Sub AAA()

    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
        If my_url Like "http://www.cnn.com" & "*" 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
    If marker = 0 Then
        MsgBox ("A matching webpage was NOT found")
    Else
        'Switch to the tab that matched and Copy the text from this website..
    End If

End Sub

【问题讨论】:

  • 也许您会更乐意跳过用户界面复制粘贴方法,而是转到临时 Internet 文件并找到 .html 文档。告诉我你是否已经排除了这种方法!
  • 您能否提供实际的 URL 和预期的输出?那可能是XY problem
  • @omegastripes,不幸的是我不能,这是一个内网地址。 elliotsvensson,我没有,我不知道该怎么做。
  • 无论如何,您可以共享 HTML 内容(已清理敏感数据的相关部分)和预期输出。
  • 如何获取 HTML 内容?我导航到临时 Internet 文件夹。我在文件中没有看到 HTML 类型

标签: vba excel parsing internet-explorer web-scraping


【解决方案1】:

他是获取IE句柄的一种方式

Option Explicit

Sub Sample()
    Dim ShellApp As Object, ShellWindows As Object
    Dim objIE As Object, objWin As Object
    Dim ieCaption As String

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

    '~~> Loop through the windows and check if it is IE
    For Each objWin In ShellWindows
        If (Not objWin Is Nothing) Then
            ieCaption = objWin.Name
            If ieCaption = "Internet Explorer" Then
                Set objIE = objWin
                Exit For
            End If
        End If
    Next

    Set ShellApp = Nothing

    '~~> If IE is found then wirk with that object
    If Not objIE Is Nothing Then
        With objIE
            Debug.Print .Document.Body.Innerhtml
        End With
    End If
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-05
    • 1970-01-01
    • 2014-01-15
    • 1970-01-01
    • 2012-11-04
    • 1970-01-01
    • 1970-01-01
    • 2020-12-26
    相关资源
    最近更新 更多