如何使用 VBA 从已经打开的网页中读取文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21407340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:39:03  来源:igfitidea点击:

How to read text from an already open webpage using VBA

vba

提问by dsauce

I have made a macro to read text from a saved html page/file. I now need to make it more advance by reading an already open webpage. would really appreciate the help

我制作了一个宏来从保存的 html 页面/文件中读取文本。我现在需要通过阅读一个已经打开的网页来让它更先进。非常感谢您的帮助

Need to replace the line

需要换线

URL = "file:///C:/test.html"

with something that will read an open webpage. I can make sure that there is only one tab open. I'm using the latest IE

带有可以读取打开网页的内容。我可以确保只有一个选项卡打开。我正在使用最新的 IE

Dim URL As String
Dim Data As String

URL = "file:///C:/test.html"

Dim ie As Object
Dim ieDoc As Object

Set ie = CreateObject("InternetExplorer.Application")
ie.navigate URL

Do Until (ie.readyState = 4 And Not ie.Busy)
    DoEvents
Loop

Set ieDoc = ie.Document

Data = ieDoc.body.innerText

回答by ron

If you know the title or url of the already open webpage you're looking for, then this code will let you control it

如果您知道要查找的已打开网页的标题或网址,则此代码将让您控制它

' Determine if a specific instance of IE is already open.
    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

        'You can use my_title of my_url, whichever you want
        If my_title Like "Put your webpage title here" & "*" Then   'identify the existing web page
            Set ie = objShell.Windows(x)
            Exit For
        Else
        End If
    Next

回答by CommonGuy

Use this code to get the currently running internet explorer (work atleast with IE9):

使用此代码获取当前运行的 Internet Explorer(至少在 IE9 上工作):

Dim ie As Object
Dim objShell As Object
Dim objWindow As Object
Dim objItem As Object

Set objShell = CreateObject("Shell.Application")
Set objWindow = objShell.Windows()
For Each objItem In objWindow
    If LCase(objItem.FullName Like "*iexplore*") Then
        Set ie = objItem
    End If
Next objItem

MsgBox ie.Document.body.innertext

回答by dee

' Add reference to 
' - Microsoft Internet Controls (SHDocVw)
' - Microsoft Shell Controls and Automation (Shell32)
' Find all running instances of IE and get web page Url
' Source: http://msdn.microsoft.com/en-us/library/windows/desktop/bb773974(v=vs.85).aspx
' Useful link: http://msdn.microsoft.com/en-us/library/windows/desktop/bb776890(v=vs.85).aspx

Sub main()
    Dim browsers
    Set browsers = GetBrowsers

    Dim browser
    Dim url
    For Each browser In browsers
        url = browser.document.Location.href
        Debug.Print CStr(url)
    Next browser
End Sub

Public Function GetBrowsers() As Collection

    Dim browsers As New Collection
    Dim shellApp As Shell32.Shell
    Dim wnds As SHDocVw.ShellWindows

    Set shellApp = New Shell
    Set wnds = shellApp.Windows

    Dim i As Integer
    Dim ie As SHDocVw.WebBrowser
    Dim name

    For i = 1 To wnds.Count
        Set ie = wnds(i)
        If ie Is Nothing Then GoTo continue
        If UCase(ie.FullName) Like "*IEXPLORE.EXE" Then
            browsers.Add ie
        End If
continue:
    Next i

    Set GetBrowsers = browsers
    Set shellApp = Nothing
End Function