I hope someone can help me....
I'm (very) new to Open XML (and programming altogther)and have been playing around with it for a couple of days now. I've been trying to link an external XML file to a word document, but am having some issues in doing this.
Basically, i'll describe exactly what i'm doing and perhaps someone can point me in the right direction:
1) Create a new word 2007 document
2) Add 3 plain text content controls from the developer tab
3) Save the document in docx format
4) Launch VBA Editor
5) Enter the following code to add a custom XML part and load an XML document:
NB - The XML file looks like this:
<?xml version="1.0" ?>
6) Bind the data in the XML nodes to the (previously created) content controls:
Dim strXPath1 As String
strXPath1 = "/Customer/CompanyName"
Dim strXPath2 As String
strXPath2 = "/Customer/Location"
Dim strXPath3 As String
strXPath3 = "/Customer/Phone"
7) Click 'save' in VBA Editor. NB: I also get the following error message: "The custom XML part has already been loaded." I can't seem to understand why this error occurs... :s
8) No changes in the content controls!!!!
Does anyone know where i'm going wrong? :( I know it's something silly, but I can't quite put my finger on it at the moment!
I'm unsure as to whether I enter the code in steps 5 & 6 into the 'Immediate' window or in the 'Code' window. Do I enter step 5 first and then save? Or do I enter both sets of code at once and then save? :s
Some assistance at grassroots level is desperately needed... :)
Thanks in advance, people!
Refer below links for details.
Thanks for your reply.
The second link that you provided is the link that I have been working through over the past couple of days. However, I still seem to be getting nowhere as, upon adding a custom XML part (ActiveDocument.CustomXMLParts.Add) and loading my XML file (ActiveDocument.CustomXMLParts(3).Load ("c:\Data.xml")), I seem to be getting stuck. I enter these two lines of code into the 'immediate' window of VBA, but it constantly brings up the following error message when I try to hit return to start a new line:
"The custom XML part has already been loaded."
The impplication of this is such that when I view the word document in ZIP format, there is no instance of a 'CustomXML' folder, i.e. the XML data store which I tried to link does not get attached to the document.
I have already referred to the first link you provided, but I found it to be slightly on the vague side when addressing the above error message.
I just think it has something to do with where/how i'm entering the code in the VBA Editor. It's so frustrating now...
I hope you can still help. :)
It works for me with minor code change. Try the following
1. In your VBA editor, add a reference to mxxml v3 dll (available at C:\WINDOWS\system32)
2. copy,paster these code
Dim XMLDOC As New MSXML2.DOMDocumentXMLDOC.Load ("c:\test\data.xml") 'check this location
Dim strXML As StringstrXML = XMLDOC.DocumentElement.XML
ActiveDocument.CustomXMLParts.Add (strXML)Dim strXPath1 As String
I guess, the problem was add and load method does the same job, hence it was saying custom xml part has already been added.Comment out your load method, you dont get this error message.
There are many ways to map xml to content controls.
1. Use word content control tool it Refer these links
2. Use System.Io.Packaging API
3. Use OpenXML SDK
Get back to me if you have any issue on these.