XML in VBA

Michael Paravicini
Michael Paravicini used Ask the Experts™
on
I am using XML in vba but not sure how to represent the following code:

<InstdAmt Ccy="CHF">15.00</InstdAmt>

I am currently using:

 Set NodeAmt = doc.createNode(NODE_ELEMENT, "Amt", nmsp)
 NodeCdtTrfTxInf.appendChild NodeAmt

          Set NodeInstdAmtCcy = doc.createNode(NODE_ELEMENT, "InstdAmtCcy", nmsp)
          NodeAmt.appendChild NodeInstdAmtCcy
          NodeInstdAmtCcy.Text = Nz(rst!Ccy)

          Set NodeInstdAmt = doc.createNode(NODE_ELEMENT, "InstdAmt", nmsp)
          NodeAmt.appendChild NodeInstdAmt
          NodeInstdAmt.Text = rst!PayAmt

which obviously is wrong. Any help is really greatly appreciated... Thanks Michael
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior Developer
Commented:
Assuming you're using MSXML2, then it's:

Option Explicit

Public Sub Test()

  Dim Document As MSXML2.DOMDocument60
  Dim Attrib As MSXML2.IXMLDOMAttribute
  Dim Child As MSXML2.IXMLDOMNode
  
  Set Document = New MSXML2.DOMDocument60
  Document.LoadXML "<root></root>"
  Set Attrib = Document.createAttribute("Ccy")
  Attrib.Value = "CHF"
  Set Child = Document.createNode(NODE_ELEMENT, "InstdAmtCcy", "")
  Child.Attributes.setNamedItem Attrib
  Child.Text = "test"
  Document.DocumentElement.appendChild Child
  Set Attrib = Nothing
  Set Child = Nothing
  Debug.Print Document.XML
  Set Document = Nothing

End Sub

Open in new window

Author

Commented:
Perfect - thank you so much for your help.. Rg Michael

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial