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

时间:2014-01-28 13:48:09

标签: 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 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

3 个答案:

答案 0 :(得分:0)

如果你知道你正在寻找的已打开网页的标题或网址,那么这段代码可以让你控制它

' 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

答案 1 :(得分:0)

使用此代码获取当前正在运行的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

答案 2 :(得分:0)

' 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