对象变量或块变量未设置XML selectSingleNode VBA的错误

时间:2014-02-26 20:58:38

标签: xml vba selectsinglenode

我有点新手(无论是在本网站还是编码)所以请耐心等待。我有一个XML文档,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
  -<LearnerFirstTimeRegistration xmlns="urn:learnerregfeedback-schema"> 
     -<Header> 
       -<Record type="1">
         <Filename>blahblahblah.xml</Filename> 
         <ProvinceID>5</ProvinceID> 
         <EmisNumber>5xxxxxxx8</EmisNumber> 
         <StatusID>2</StatusID> 
         <TransactionCategoryID>4</TransactionCategoryID> 
         <SequenceNumber>1</SequenceNumber> 
         <DateCreated>2014-02-26</DateCreated> 
         <CreatedBy>blahblahblah</CreatedBy> 
        </Record> 
      </Header> 
    -<LearnerInformation> ....  etc etc

使用VBA我只是试图在msgbox中显示SequenceNumber节点中的值(在这种情况下= 1)

我正在使用:

Private Sub Command317_Click()

 Dim objNodeList As IXMLDOMNodeList
 Dim objNode As IXMLDOMNode
 Dim xDoc As MSXML2.DOMDocument30

 Set xDoc = New MSXML2.DOMDocument30

 xDoc.LoadXML ("\\location\blahblahblah.xml")

 MsgBox xDoc.selectSingleNode("SequenceNumber").nodeName

End Sub

它给我一个运行时错误:对象变量或未设置块变量 从我在网上看到的,它似乎暗示它,因为它试图返回一个空值。但是正如你所看到的,SequenceNumber不是null,它是1.

非常感谢任何帮助

1 个答案:

答案 0 :(得分:0)

MsgBox xDoc.selectSingleNode("//SequenceNumber").nodeName

MsgBox xDoc.selectSingleNode("//SequenceNumber").Text

如果你想要元素内容。

编辑:

这是适用于我的代码:

Sub Test()

    Dim oDoc As New MSXML2.DOMDocument30
    Dim XML As String

    XML = ActiveSheet.Range("A1").Value

    oDoc.validateOnParse = True
    oDoc.LoadXML XML

    Debug.Print oDoc.SelectSingleNode("//SequenceNumber").Text '>> 1

End Sub

我正在从工作表单元格加载此XML:

 <?xml version="1.0" encoding="UTF-8"?>
  <LearnerFirstTimeRegistration  xmlns="urn:learnerregfeedback-schema"> 
     <Header> 
       <Record type=""1"">
         <Filename>blahblahblah.xml</Filename> 
         <ProvinceID>5</ProvinceID> 
         <EmisNumber>5xxxxxxx8</EmisNumber> 
         <StatusID>2</StatusID> 
         <TransactionCategoryID>4</TransactionCategoryID> 
         <SequenceNumber>1</SequenceNumber> 
         <DateCreated>2014-02-26</DateCreated> 
         <CreatedBy>blahblahblah</CreatedBy> 
        </Record> 
      </Header> 
    </LearnerFirstTimeRegistration>