我很努力地通過一個XML文件解析excel。我發現了大量的例子,但沒有一個看起來與我正在尋找的完全相同,而且我似乎無法越過錯誤「Object variable or With block variable not set」如何在Excel中使用vba訪問xml中的特定元素和屬性?
xml格式良好看起來像以下:
<xml tag>
<PLMXML>
<WorkflowTemplate name="">
<argument name="">
</argument>
</WorkflowTemplate >
<WorkflowTemplate name="">
等
我試圖使用VBA獲得所有孩子的名字的價值,並分別得到的參數名稱。我不斷收到錯誤與此代碼:
Dim xmlDoc As MSXML2.DOMDocument
Dim xmlElement As MSXML2.IXMLDOMElement
Dim xmlNode As MSXML2.IXMLDOMNode
Dim xmlAttribute As MSXML2.IXMLDOMAttribute
Set xmlDoc = New MSXML2.DOMDocument
xmlDoc.async = False
xmlDoc.validateOnParse = False
'ENTER THE PATH WHERE THE XML Workflow DOCUMENT IS STORED:
Dim DocumentPath As String
DocumentPath = InputBox("Enter the full path for the xml workflow document, example: C:\workflows\workflowseasy.xml", "Workflow XML File path", "C:\workflows\workflowseasy.xml")
xmlDoc.Load (DocumentPath)
Set xmlElement = xmlDoc.DocumentElement
Set xmlNode = xmlElement.SelectSingleNode("WorkflowTemplate[0]")
Set xmlAtribute = xmlNode.Attributes.getNamedItem("name")
我不清楚如何獲取數據使用該解析器在Excel VBA中在文檔中。任何幫助將不勝感激。我目前在參考資料中選擇了Microsoft XML v6.0。
UPDATE
我已經挖成以上,並已經提出了下面的代碼,但我仍然得到了同樣的錯誤:
Dim xmlDoc As MSXML2.DOMDocument60
Dim xmlRoot As MSXML2.IXMLDOMNode
Dim xmlTemplate As MSXML2.IXMLDOMNode
Dim xmlAttributes As MSXML2.IXMLDOMNamedNodeMap
Dim xmlName As MSXML2.IXMLDOMNode
Dim xmlChildren As MSXML2.IXMLDOMNodeList
Dim xmlChild As MSXML2.IXMLDOMNode
Dim intI As Long
intI = 1
Set xmlDoc = New MSXML2.DOMDocument60
xmlDoc.async = False
xmlDoc.validateOnParse = False
'ENTER THE PATH WHERE THE XML Workflow DOCUMENT IS STORED:
Dim DocumentPath As String
DocumentPath = InputBox("Enter the full path for the xml workflow document, example: C:\workflows\workflowseasy.xml", "Workflow XML File path", "C:\workflows\workflowseasy.xml")
xmlDoc.Load (DocumentPath)
Set xmlRoot = xmlDoc.DocumentElement *****these say they are empty when debugging
Set xmlChildren = xmlRoot.ChildNodes *****these say they are empty when debugging
For Each xmlTemplate In xmlChildren *****error occures here
If xmlTemplate.nodeName = "WorkflowTemplate" Then
Set xmlAttributes = xmlTemplate.Attributes
Set xmlName = xmlAttributes.getNamedItem("name")
ActiveSheet.Cells(int1, 1).Value = xmlName.Text
Set xmlChildren = xmlTemplate.ChildNodes
intI = intI + 1
End If
Next xmlTemplate
最後更新* *
想通了。該文件的加載是問題。出於某種原因傳遞它從msg框中的字符串不起作用,但從gui文件選擇器傳遞它。這是我最終使用的代碼。
Dim xmlDoc As MSXML2.DOMDocument60
Dim xmlRoot As MSXML2.IXMLDOMNode
Dim xmlTemplate As MSXML2.IXMLDOMNode
Dim xmlAttributes As MSXML2.IXMLDOMNamedNodeMap
Dim xmlName As MSXML2.IXMLDOMNode
Dim xmlChildren As MSXML2.IXMLDOMNodeList
Dim xmlChild As MSXML2.IXMLDOMNode
Dim intI As Long
intI = 1
Set xmlDoc = New MSXML2.DOMDocument60
xmlDoc.async = False
xmlDoc.validateOnParse = False
'ENTER THE PATH WHERE THE XML Workflow DOCUMENT IS STORED:
Dim DocumentPath As String
With Application.FileDialog(msoFileDialogOpen)
.Title = "Choose File"
.AllowMultiSelect = False
.Show
'DocumentPath.Show
DocumentPath = .SelectedItems(1)
End With
xmlDoc.Load (DocumentPath)
Set xmlRoot = xmlDoc.DocumentElement
Set xmlChildren = xmlRoot.ChildNodes
For Each xmlTemplate In xmlChildren
If xmlTemplate.nodeName = "WorkflowTemplate" Then
Set xmlAttributes = xmlTemplate.Attributes
Set xmlName = xmlAttributes.getNamedItem("name")
ActiveSheet.Cells(int1, 1).Value = xmlName.Text
Set xmlChildren = xmlTemplate.ChildNodes
intI = intI + 1
End If
Next xmlTemplate
目前該分配值部分的代碼休息,而是通過變量拉動正確的價值觀,並在XML信息正確抽取的代碼去。
你會得到什麼錯誤? –
當我到達Set xmlNode = xmlElement.SelectSingleNode時,它失敗。 – user2513952
我試過用這個來看看我是否至少可以得到值,但是這似乎沒有給我任何東西。完全跳過for循環Set xmlList = xmlDoc。SelectNodes(「// WorkflowTemplate」) 對於xmlList中的每個xmlNode Debug.Print xmlNode.Text Next xmlNode – user2513952