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

如何使用VBA从已打开的网页中读取文本,vba,Vba,我制作了一个宏,用于从保存的html页面/文件中读取文本。我现在需要通过阅读一个已经打开的网页使它更先进。非常感谢你的帮助 需要更换线路吗 URL = "file:///C:/test.html" 可以阅读打开的网页。我可以确保只有一个选项卡处于打开状态。我用的是最新的IE Dim URL As String Dim Data As String URL = "file:///C:/test.html" Dim ie As Object Dim ieDoc As Object Set i

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

需要更换线路吗

URL = "file:///C:/test.html"
可以阅读打开的网页。我可以确保只有一个选项卡处于打开状态。我用的是最新的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

如果您知道正在查找的已打开网页的标题或url,则此代码将允许您控制它

' 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

使用此代码获取当前正在运行的internet explorer(至少使用IE9):


这不是一个实际指定从中获取数据的URL的选项吗?您所说的
已打开网页
是什么意思?@OlleSjögren ready open webpage=存在一个在IE中打开的网页=在宏打开之前,网页已在浏览器中打开fired@ChrisProsser我无权调用该网页的第二个实例…我需要利用已经打开的URL代码,那么您需要更改的不仅仅是URL代码。您现有的代码似乎正在为本地html文件打开一个新的IE实例。我不确定是否可以从VBA读取现有浏览器会话中的活动。
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
' 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