Home > database >  VBA OUTLOOK - READ XML FILE
VBA OUTLOOK - READ XML FILE

Time:11-16

Sample XML file (insert path):

<FilePath>
<TemplatePath> "C:\Users\test.txt" </TemplatePath>
</FilePath>

I am trying to take the value of but i always get a run-time error '91', this is the code:

Sub testxml()



Set oXMLFile = CreateObject("Microsoft.XMLDOM")
oXMLFile.Load ("C:\Users\Config.xml")


Set prova = oXMLFile.SelectNodes("//FilePath/TemplatePath").item(0).Text
Debug.Print prova

I also cannot find proper documentation (I've tried to see on Microsoft learn but it doesn't explain very well those things).

Thank you!

CodePudding user response:

Set prova = is incorrect as .Text returns a string not an object reference, instead:

dim text as string
text = oXMLFile.SelectNodes("//FilePath/TemplatePath").Item(0).Text

Alternatively if there is always a single TemplatePath element simply:

debug.print oXMLFile.SelectSingleNode("//FilePath/TemplatePath").Text

If there are multiple nodes, loop them:

Set prova = oXMLFile.SelectNodes("//FilePath/TemplatePath")
   
For Each node In prova
    debug.print node.Text
Next node

CodePudding user response:

You may find the XML DOM Methods described in MSDN.

To read the content you may use the following code (without item[0]):

oXMLFile.SelectSingleNode("//FilePath/TemplatePath").Text
  • Related