Adding attribute to xml custompart in docx Word document at run time using VBA

Posted on 2014-08-07
Medium Priority
Last Modified: 2016-05-12
I am saving data included in Word document in XML format in the ActiveDocument.CustomXmlParts.
I can add node to the XML structure, using VBA code,  but I didn't succeed to add new attribute. I've search a lot but didn't find any reference to this.
Here is the code:

Set MyDoc = ActiveDocument
xml = "<Root><Cities><City id='1'>NY</City><City id='2'>Boston</City></Cities></Root>"
Set MyPart = MyDoc.CustomXMLParts.Add(xml)
Set ParentNode = MyPart.SelectSingleNode("//Root/Cities")
MyPart.AddNode ParentNode, "City", , , , "LA"

Open in new window

How to add a node at runtime with an attribute: <City id='3'>LA</City>

Thank you

Question by:nava1000
LVL 35

Accepted Solution

Robert Schutt earned 2000 total points
ID: 40248394
I'm not really able to test this other than in the VBA editor with a watch, but this seems to work:

After your line 6 (I added the code block above), add this line:
MyPart.AddNode ParentNode.LastChild, "id", , , msoCustomXMLNodeAttribute, 3

Open in new window


Author Comment

ID: 40250834
Thank you very much.

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IT Consulting can be a lucrative business, but building a client base and reaching a level of financial comfort and sustainability is not always easy.  In fact, it can be extremely difficult. I’m not going to focus on your skills and technical qu…
Building a successful professional career is a long and difficult journey, especially in case if your decisions are not chosen carefully. For example, if you think that you can get to the desired position without experience and apply for it, your ch…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses
Course of the Month14 days, 19 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question