2014-01-28 3 views
1

Я сделал макрос для чтения текста с сохраненной страницы/файла html. Теперь я должен сделать это более продвинутым, прочитав уже открытую веб-страницу. действительно ценю помощьКак читать текст с уже открытой веб-страницы с помощью VBA

нужно заменить строку

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 
+0

Разве это не вариант на самом деле указать URL, чтобы получить данные из? – ChrisProsser

+0

Что вы подразумеваете под 'уже открытой веб-страницей'? –

+0

@ OlleSjögren уже открыта webpage = есть веб-страница, которая открывается в IE = веб-страница была открыта в браузере до того, как макрос был запущен – dsauce

ответ

0

Если вы знаете название или 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 
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 
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 
Смежные вопросы