gpt4 book ai didi

VBA 默认命名空间错误

转载 作者:行者123 更新时间:2023-12-02 11:58:32 25 4
gpt4 key购买 nike

我正在尝试实例化 Dom 文档的根节点。不过,我将其命名为 xbrl 并且该名称位于默认命名空间中,即 xmlns="http://www.xbrl.org/2003/instance"

根据previous post-answer MSXML is buggy (answer of barrowc)当涉及到默认命名空间时。所以我必须对我的代码进行一些修改。这些地方

objXMLDoc.LoadXML (objXMLHTTP.responseText)

替换为

objXMLDoc.LoadXML objXMLHTTP.responseText
objXMLDoc.setProperty "SelectionNamespaces", "xmlns:r='http://www.xbrl.org/2003/instance'"

还有

Dim objXMLHTTP As New MSXML2.XMLHTTP
Dim objXMLDoc As New MSXML2.DOMDocument

替换为

Dim objXMLHTTP As New MSXML2.XMLHTTP60
Dim objXMLDoc As New MSXML2.DOMDocument60

数字60象征版本6.0

因此,当我进行这些修改时,宏可以正常工作而不会出现错误。但现在它只是有时有效。如果没有,它会给我一个

运行时错误-2147467259(80004005)':

引用未声明的命名空间前缀:'us-gaap。'

我无法理解宏崩溃的原因并相信这是一个错误。

你能帮忙吗?

出于完整性的考虑,整个宏在下面提交

Sub READSITE()

Dim IE As InternetExplorer
Dim els, el, colDocLinks As New Collection
Dim lnk, res
Dim Ticker As String
Dim colXMLPaths As New Collection
Dim XMLElement As String

Set IE = New InternetExplorer

IE.Visible = False

Ticker = Worksheets("Sheet1").Range("A1").Value

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

Set els = IE.Document.getelementsbytagname("a")
For Each el In els
If Trim(el.innertext) = "Documents" Then
colDocLinks.Add el.href
End If
Next el

For Each lnk In colDocLinks
LoadPage IE, CStr(lnk)
For Each el In IE.Document.getelementsbytagname("a")
If el.href Like "*[0-9].xml" Then
Debug.Print el.innertext, el.href
colXMLPaths.Add el.href
End If
Next el
Next lnk

XMLElement = Range("C1").Value

'For each link, open the URL and display the Debt Instrument Insterest Rate
For Each lnk In colXMLPaths
res = GetData(CStr(lnk), XMLElement)
With Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
.NumberFormat = "@"
.Value = Ticker
.Offset(0, 1).Value = lnk
.Offset(0, 2).Value = res
End With
Next lnk

End Sub

Function GetData(sURL As String, sXMLElement As String)
Dim strXMLSite As String
Dim objXMLHTTP As New MSXML2.XMLHTTP60
Dim objXMLDoc As New MSXML2.DOMDocument60
Dim objXMLNodexbrl As MSXML2.IXMLDOMNode
Dim objXMLNodeElement As MSXML2.IXMLDOMNode
Dim objXMLNodeStkhldEq As MSXML2.IXMLDOMNode

GetData = "?" 'No data from XML
objXMLHTTP.Open "GET", sURL, False '<<EDIT: GET the site
objXMLHTTP.send
objXMLDoc.LoadXML (objXMLHTTP.responseText)
objXMLDoc.setProperty "SelectionNamespaces", "xmlns:r='http://www.xbrl.org/2003/instance'"

Set objXMLNodexbrl = objXMLDoc.SelectSingleNode("r:xbrl")

Set objXMLNodeElement = objXMLNodexbrl.SelectSingleNode(sXMLElement)

If Not objXMLNodeElement Is Nothing Then
GetData = objXMLNodeElement.Text
End If
End Function

Sub LoadPage(IE As Object, url As String)
IE.Navigate url
Do While IE.Busy Or IE.ReadyState <> READYSTATE_COMPLETE
DoEvents
Loop
End Sub
<小时/>

这也是一个奇怪且令人沮丧的注释,如果我在更正前的状态下更改宏 according to the modifications由 barrowc 提供给我,我现在可以看到该宏有效!

最佳答案

我能够通过 checkin Diff Checker 迅速终止该错误我之前拥有的大量代码和代码示例。看来您需要的只是从这些行中删除 60 (天知道为什么......)

Dim objXMLHTTP As New MSXML2.XMLHTTP60
Dim objXMLDoc As New MSXML2.DOMDocument60

关于VBA 默认命名空间错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22150239/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com