views:

160

answers:

1

In this tutorial (and many others), there's an integer in the CustomXMLParts object collection Load method that I can't find an explanation for. The Word 2007 VBA Reference doesn't seem to list the Load method either:

  ''# Load CustomerData.xml file
  ActiveDocument.CustomXMLParts.Add
  ActiveDocument.CustomXMLParts(4).Load ("c:\CustomerData.xml") 

What does the 4 represent?

+1  A: 

There are always three built-in CustomXMLParts in every .docx (created by Word 2007/2010 - not necessarily a .docx created by the Open XML SDK). Namely:

<cp:coreProperties xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties"&gt;&lt;dc:creator&gt;&lt;/dc:creator&gt;&lt;cp:keywords/&gt;&lt;dc:description/&gt;&lt;dc:subject/&gt;&lt;dc:title/&gt;&lt;cp:category/&gt;&lt;cp:contentStatus/&gt;&lt;/cp:coreProperties&gt;

<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties"&gt;&lt;Company/&gt;&lt;Manager/&gt;&lt;/Properties&gt;

<CoverPageProperties xmlns="http://schemas.microsoft.com/office/2006/coverPageProps"&gt;&lt;PublishDate/&gt;&lt;Abstract/&gt;&lt;CompanyAddress/&gt;&lt;CompanyPhone/&gt;&lt;CompanyFax/&gt;&lt;CompanyEmail/&gt;&lt;/CoverPageProperties&gt;

So 4 here just means, after you've done ActiveDocument.CustomXMLParts.Add to add a fourth one "get the fourth one". If you had more, you would just use the next available index number. Instead of 4, I'd probably just use this instead:

Dim ap As Document
Set ap = ActiveDocument
ap.CustomXMLParts.Add
ap.CustomXMLParts(ap.CustomXMLParts.Count).Load ("C:\CustomerData.xml")
Otaku
Thanks so much, one more quick question: if I want to have content controls in a .dotm template that are bound to an XML file, do I need to call the CustomXMLParts.Load (and the SetMapping method for each control) each time the document loads (i.e. Sub onload() ) or can I just save it after it's been executed?
gravyface
You should just have to map it once, not each time. But if your XML data is changing between closing/opening documents, you'll want to sink the XMLNode events (bottom of this page): http://msdn.microsoft.com/en-us/library/aa433905(office.12).aspx.
Otaku