wordpress hit counter
Word 2007 Content Control Mapping - Custom XML - WordprocessingML - Formats - OpenXML Developer

Word 2007 Content Control Mapping - Custom XML

Formats

Discussions about working with different Open XML Formats

Word 2007 Content Control Mapping - Custom XML

  • rated by 0 users
  • This post has 4 Replies |
  • 0 Followers
  • Hi,

    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:

    ActiveDocument.CustomXMLParts.Add

    ActiveDocument.CustomXMLParts(3).Load ("c:\T\Data.xml")

    NB - The XML file looks like this:

    <?xml version="1.0" ?>

    <Customer>

     <CompanyName>Timothy Winters</CompanyName>

     <Location>Springfield</Location>

     <Phone>02476144524</Phone>

    </Customer>

    6) Bind the data in the XML nodes to the (previously created) content controls:

    Dim strXPath1 As String

    strXPath1 = "/Customer/CompanyName"

    ActiveDocument.ContentControls(1).XMLMapping.SetMapping strXPath1

    Dim strXPath2 As String

    strXPath2 = "/Customer/Location"

    ActiveDocument.ContentControls(2).XMLMapping.SetMapping strXPath2

    Dim strXPath3 As String

    strXPath3 = "/Customer/Phone"

    ActiveDocument.ContentControls(3).XMLMapping.SetMapping strXPath3

    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!

    ~ESIS

  • Hi,

    Refer below links for details.

    http://openxmldeveloper.org/forums/thread/337.aspx

    http://msdn2.microsoft.com/hi-in/library/bb510135.aspx


    Sheela


     

  • Hi Sheela,

     

    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. :)

    Regards,

     

    ~ESIS

  • Hi,

    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.DOMDocument
    XMLDOC.Load ("c:\test\data.xml")  'check this location

    Dim strXML As String
    strXML = XMLDOC.DocumentElement.XML

    ActiveDocument.CustomXMLParts.Add (strXML)
    Dim strXPath1 As String

    strXPath1 = "/Customer/CompanyName"

    ActiveDocument.ContentControls(1).XMLMapping.SetMapping strXPath1

    Dim strXPath2 As String

    strXPath2 = "/Customer/Location"

    ActiveDocument.ContentControls(2).XMLMapping.SetMapping strXPath2

    Dim strXPath3 As String

    strXPath3 = "/Customer/Phone"

    ActiveDocument.ContentControls(3).XMLMapping.SetMapping strXPath3

    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

     http://www.codeplex.com/wiki/view.aspx?projectname=dbe

    2. Use System.Io.Packaging API

    3. Use OpenXML SDK


    Get back to me if you have any issue on these.


    Sheela

     

     

     

     

  • I have a similar problem.  After I run or display a docm with content controls and VB script that loads CustomXMLParts(4).load one time, I continue to get the error "This custom XML part has already been loaded" every other time, I try to load the document.  I have been fooling around with Open XML for Word for weeks and it seems like it never works as I see in the videos and tutorials.   
Page 1 of 1 (5 items)