Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在目录中的HTML文件夹中按标记名搜索元素_Html_Vba_Excel_Excel 2010 - Fatal编程技术网

在目录中的HTML文件夹中按标记名搜索元素

在目录中的HTML文件夹中按标记名搜索元素,html,vba,excel,excel-2010,Html,Vba,Excel,Excel 2010,如您所见,这是一段代码摘录,它加载一个HTML文档,然后通过使用标记名搜索元素来获取元素 'STATEMENT_1 LoadPage IE, "https://www.sec.gov/cgi-bin/browse-edgar?" & _ "action=getcompany&CIK=" & Ticker & "&type=" & InfoType & _

如您所见,这是一段代码摘录,它加载一个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从此位置转到/获取?

这里有一种使用IE与本地文件交互的方法

HTML文件保存到桌面

结果:


在IE10和Excel 2010上测试

我没有尝试过它是否有效,但将代码的URL更改为本地路径是否有效?替换action=getcompany&CIK=&Ticker&&type=&InfoType&&&dateb=&owner=exclude&count=20,带有C:\vsti\yourthmlname.htmlohh,可以完成此功能!!!1这仍然需要使用IE:-@Portland Runner Hah我不确定它是否有效,而且我猜不使用IE,Codo指的是使用浏览器在线打开网页,但这只是我的猜测!感谢您指出thouNo它不工作Internet Explorer打开包含财务信息的HTML文件时,所有的麻烦都散开了:P
<HTML>
<HEAD>
  <Title>My Example</Title>
</HEAD>
<BODY>
  <div class="find-me">
    You Found Me
  </div>
</BODY>
</HTML>
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