none
Exporting XML Mapped Content from Word 2007 RRS feed

  • Question

  • I have experience mapping and exporting Excel 2007 content using XML Schemas.  I notice that I can map content in Word 2007 documents, but I have found no information on how to export the mapped content into an XML file.  The equivalent "Export" function does not exist in Word as it does in Excel.

    Is there a VB macro or some other command that allows me to do this from Word 2007?

    Wednesday, March 21, 2012 5:41 PM

Answers

  • Hi FredX500,

    As far as I know, we can get the xml information from the ActiveDocument.CustomXMLParts(4).XML , it can retrieve the xml content. Then, you can write to xml file.

    Hope this can help you and just feel free to follow up after you have tried.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us

    • Edited by Bruce Song Friday, March 23, 2012 5:41 AM
    • Marked as answer by Bruce Song Monday, April 2, 2012 8:06 AM
    Friday, March 23, 2012 5:35 AM
  • FredX500, I found that we can use the file IO API in VBA which seems ok to export the xml information to be xml file, please refer to the following code snippet:

      ' add the custom xml file to the file
    Private Sub CommandButton1_Click()
    If ActiveDocument.CustomXMLParts.Count = 4 Then
        ActiveDocument.CustomXMLParts(4).Delete
    End If
        ' Add a new, empty custom XML part to the document.
        ActiveDocument.CustomXMLParts.Add
        ' Load XML from CustomerData.xml file.
        ActiveDocument.CustomXMLParts(4).Load ("CustomerData.xml")
    End Sub


    ' map xml file to content controls
    Private Sub CommandButton2_Click()
    Dim strXPath1 As String
    strXPath1 = "/Customer/CompanyName"
    ActiveDocument.ContentControls(1).XMLMapping.SetMapping strXPath1

    Dim strXPath2 As String
    strXPath2 = "/Customer/ContactName"
    ActiveDocument.ContentControls(2).XMLMapping.SetMapping strXPath2

    Dim strXPath3 As String
    strXPath3 = "/Customer/ContactTitle"
    ActiveDocument.ContentControls(3).XMLMapping.SetMapping strXPath3

    Dim strXPath4 As String
    strXPath4 = "/Customer/Phone"
    ActiveDocument.ContentControls(4).XMLMapping.SetMapping strXPath4
    End Sub

    Private Sub CommandButton3_Click()
        Dim xmlFile As String
        xmlFile = "ExportedXmlFile.xml"

        Open xmlFile For Output As #1
            Print #1, ActiveDocument.CustomXMLParts(4).XML
        Close #1
    End Sub

    And the CustomerData.xml content looks like this:

    <?xml version="1.0"?>
    <Customer> 
       <CompanyName>Adventure Works</CompanyName> 
       <ContactName>Terry Adams</ContactName>
       <ContactTitle>Sales Representative</ContactTitle>
       <Phone>030-0074321</Phone>
    </Customer>

    Hope this can help you and feel free to follow up after you have tried.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us

    • Edited by Bruce Song Monday, March 26, 2012 12:40 PM add more code snippet
    • Marked as answer by Bruce Song Monday, April 2, 2012 8:06 AM
    Monday, March 26, 2012 12:36 PM

All replies

  • Hi FredX500,

    As far as I know, we can get the xml information from the ActiveDocument.CustomXMLParts(4).XML , it can retrieve the xml content. Then, you can write to xml file.

    Hope this can help you and just feel free to follow up after you have tried.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us

    • Edited by Bruce Song Friday, March 23, 2012 5:41 AM
    • Marked as answer by Bruce Song Monday, April 2, 2012 8:06 AM
    Friday, March 23, 2012 5:35 AM
  • Ok.  I guess that is a lot more challenging than Excel.  It will take some time before I have something to report back on.  Thanks for pointing me in the right direction!

    Friday, March 23, 2012 4:27 PM
  • FredX500, I found that we can use the file IO API in VBA which seems ok to export the xml information to be xml file, please refer to the following code snippet:

      ' add the custom xml file to the file
    Private Sub CommandButton1_Click()
    If ActiveDocument.CustomXMLParts.Count = 4 Then
        ActiveDocument.CustomXMLParts(4).Delete
    End If
        ' Add a new, empty custom XML part to the document.
        ActiveDocument.CustomXMLParts.Add
        ' Load XML from CustomerData.xml file.
        ActiveDocument.CustomXMLParts(4).Load ("CustomerData.xml")
    End Sub


    ' map xml file to content controls
    Private Sub CommandButton2_Click()
    Dim strXPath1 As String
    strXPath1 = "/Customer/CompanyName"
    ActiveDocument.ContentControls(1).XMLMapping.SetMapping strXPath1

    Dim strXPath2 As String
    strXPath2 = "/Customer/ContactName"
    ActiveDocument.ContentControls(2).XMLMapping.SetMapping strXPath2

    Dim strXPath3 As String
    strXPath3 = "/Customer/ContactTitle"
    ActiveDocument.ContentControls(3).XMLMapping.SetMapping strXPath3

    Dim strXPath4 As String
    strXPath4 = "/Customer/Phone"
    ActiveDocument.ContentControls(4).XMLMapping.SetMapping strXPath4
    End Sub

    Private Sub CommandButton3_Click()
        Dim xmlFile As String
        xmlFile = "ExportedXmlFile.xml"

        Open xmlFile For Output As #1
            Print #1, ActiveDocument.CustomXMLParts(4).XML
        Close #1
    End Sub

    And the CustomerData.xml content looks like this:

    <?xml version="1.0"?>
    <Customer> 
       <CompanyName>Adventure Works</CompanyName> 
       <ContactName>Terry Adams</ContactName>
       <ContactTitle>Sales Representative</ContactTitle>
       <Phone>030-0074321</Phone>
    </Customer>

    Hope this can help you and feel free to follow up after you have tried.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us

    • Edited by Bruce Song Monday, March 26, 2012 12:40 PM add more code snippet
    • Marked as answer by Bruce Song Monday, April 2, 2012 8:06 AM
    Monday, March 26, 2012 12:36 PM