2014-05-13 85 views
0

如您所見,這是一個加載HTML文檔的代碼摘錄,然後通過使用標籤名稱搜索它們來獲取元素。在目錄中的HTML文件夾中按標籤名稱搜索元素

'STATEMENT_1 
LoadPage IE, "https://www.sec.gov/cgi-bin/browse-edgar?" & _ 
         "action=getcompany&CIK=" & Ticker & "&type=" & InfoType & _ 
         "&dateb=&owner=exclude&count=20" 

     'STATEMENT_2 
     Set els = IE.Document.getelementsbytagname("a") 

但是,當你需要利用Internet Explorer去取一個HTML文檔,你有你的目錄裏面的HTML什麼?

例如,如果我有目錄C:\vsti目錄中的HTML文件,我如何指示VBA從此位置進行取/取?

+1

我沒有,如果它的作品,但不嘗試改變你的代碼的URL,以本地路徑的作品? (取代**「https://www.sec.gov/cgi-bin/browse-edgar?」&_ 「action = getcompany&CIK =」&Ticker&「&type =」&InfoType&_ 「&dateb =&owner =排除與計數= 20「**與**」C:\ vsti \ yourhtmlname.html「**) – Alex

+0

Ohhhh,可以做的伎倆!!! 1 – Codo

+0

這仍然需要使用IE :-) –

回答

0

以下是使用IE與本地文件進行交互的一種方法。

HTML文件(保存到桌面)

<HTML> 
<HEAD> 
    <Title>My Example</Title> 
</HEAD> 
<BODY> 
    <div class="find-me"> 
    You Found Me 
    </div> 
</BODY> 
</HTML> 

Excel的VBA(如圖中的三種方法)

Sub GetData() 
    Dim IE As InternetExplorer 

    'Create InternetExplorer Object 
    Set IE = New InternetExplorerMedium 

    ' You can coment Next line To see page load 
    IE.Visible = False 

    'Set URL to local file 
    IE.Navigate2 "C:\Users\PortlandRunner\Desktop\index.html" 

    ' Wait while IE loading... Probably don't need this since it's local 
    Do While IE.Busy 
     Application.Wait DateAdd("s", 1, Now) 
    Loop 

    'Option A 
    Dim foundA As String 
    foundA = IE.Document.getElementsByClassName("find-me")(0).innerText 
    MsgBox foundA 

    'Option B 
    Dim foundB As Variant 
    Set foundB = IE.Document.getElementsByClassName("find-me") 
    MsgBox foundB(0).textContent 

    'Option C 
    Dim tag 
    Dim tags As Object 
    Set tags = IE.Document.getElementsByTagName("*") 

    For Each tag In tags 
     If tag.className = "find-me" Then 
      MsgBox tag.innerText 
      Exit For 
     End If 
    Next tag 

    'Show IE 
    'IE.Visible = True 

    ' Clean up 
    Set IE = Nothing 
End Sub 

結果:

enter image description here

測試IE10中& Excel 2010中

相關問題