Question

I am exploring XML w/ MS Word as a solution for generating forms with dynamic content, but am finding it difficult to do what would seem to be an easy task.

Using the example from MSDN

http://msdn.microsoft.com/en-us/library/office/gg605188%28v=office.14%29.aspx

Say I open my xml document, and change <CompanyName>Adventure Works</CompanyName> to <CompanyName>Gadget Co.</CompanyName>. How do I get the changed values in the .xml document to be reflected in my Word document?

I have tried:

1) Saving, Closing, and Re-Opening the Word doc.

2) Executing the ActiveDocument.CustomXMLParts(4).Load command again. (Throws an already loaded error, even after closing and opening.)

3) Remapping the controls with the ActiveDocument.ContentControls(#).XMLMapping.SetMapping procedure. (No error, but no change to update content.)

It seems that most of the XML and Office Suite documentation is geared towards .Net development. I don't really want to branch out into .Net for this solution, but it seems that there is much more limited support from VBA.

Ultimately, the final output is to be a mail merge type doc with several nested documents to be individually parsed and saved as pdf. The content will be dynamic, with variable length tables and dynamically generated checkboxes (with corresponding text content controls). At this point, I'm not sure if Word and XML is the way to go, and am considering a VBA based MS Publisher solution.

Any advice on this is much appreciated.

Was it helpful?

Solution

Delete the mapping, then recreate it:

Dim s as String
s = ActiveDocument.ContentControls(#).XMLMapping.XPath
ActiveDocument.ContentControls(#).XMLMapping.Delete
ActiveDocument.ContentControls(#).XMLMapping.SetMapping s

OTHER TIPS

You can iterate through all controls and redo the mapping, using the new CustomXmlPart.

Dim xmlPart As CustomXMLPart
Dim cc As ContentControl

Set xmlPart = ActiveDocument.CustomXMLParts.Add
xmlPart.Load (c:/temp/test.xml)

For Each cc In ActiveDocument.ContentControls
    cc.XMLMapping.SetMapping cc.XMLMapping.XPath, cc.XMLMapping.PrefixMappings, xmlPart
Next cc
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top