Quantcast
Channel: Open XML Format SDK forum
Viewing all articles
Browse latest Browse all 1288

Manipulating Word Docs using OpenXML

$
0
0

Hello,

I have a few MS Word documents I am attempting to manipulate via OpenXML 2.0 but having some difficulty. I am hoping someone can assist w/ some the issues listed below:

1. The MS Word documents are created from Word template files (.dotx). However, I am receiving the following error after creating the doc from the template:

Error message: "The element has unexpected child element 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:attachedTemplate'."
XPath - "/w:settings[1]"

The code in creating a document from the template is as follows:

If File.Exists(sFile.Replace("dotx", "docx")) Then File.Delete(sFile.Replace("dotx", "docx"))
        File.Copy(sFile, sFile.Replace("dotx", "docx"))
        'create word doc
        moWordDoc = WordprocessingDocument.Open(sFile.Replace("dotx", "docx"), True)
        'change doc type, which ensures the document is no longer marked as a Template but rather as a Document
        moWordDoc.ChangeDocumentType(DocumentFormat.OpenXml.WordprocessingDocumentType.Document)
        oAttachedTemplate = New AttachedTemplate
        oAttachedTemplate.Id = "relationId1"
        'append the AttachedTemplate to the DocumentSettingsPart
        oMainPart = moWordDoc.MainDocumentPart
        oDocSettings = oMainPart.DocumentSettingsPart
        oDocSettings.Settings.Append(oAttachedTemplate)
        oDocSettings.AddExternalRelationship("http://schemas.openxmlformats.org/officeDocument/2006/relationships/attachedTemplate", _
                                             New Uri(sFile, UriKind.Absolute), "relationId1")

I've also tried creating the document w/out attaching the template file to the doc but the document will open in MS Word w/ empty data.

2. A couple of the Word templates contain various background images in the tables of the template. The images act as background colors for the tables are and OpenXML doesn't seem to like. The following error is returned when validating the document:

Error message: The 'urn:schemas-microsoft-com:office:office:gfxdata' attribute is not declared.
XPath - "/w:document[1]/w:body[1]/w:tbl[1]/w:tr[1]/w:tc[1]/w:p[1]/w:r[2]/w:pict[1]/v:rect[1]"

3. Lastly, in each of the Word templates, OpenXML doesn't seem to be processing the first cell in the first row of the first table in the template. Each template contains a bookmark in the first row and cell of the table. The error returned is as follows:

Error message - "The element has invalid child element 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:r'."
XPath - "/w:document[1]/w:body[1]/w:tbl[1]"

The Run statement appears to be ok and is used throughout the document to fill bookmarks. Here's a sample of the code:


If m_arrBookmarkStarts.ContainsKey("client_name") Then
                oBookmarkStart = m_arrBookmarkStarts("client_name")
                id = oBookmarkStart.Id.Value
                oBookmarkEnd = m_arrBookmarkEnds(id)
                oText = New Text(moInvoice.Client_fname & " " & moInvoice.Client_lname)
                oElement = New Run(oText)
                oBookmarkStart = RemoveBookmarkText(oBookmarkStart, oBookmarkEnd)
            End If


Private Function RemoveBookmarkText(ByVal oBookmarkStart As BookmarkStart, ByVal oBookmarkEnd As BookmarkEnd)
        Dim oElement As DocumentFormat.OpenXml.OpenXmlElement
        Dim oNextElem As DocumentFormat.OpenXml.OpenXmlElement


        If Not IsNothing(oBookmarkStart.ColumnFirst) Then Return oBookmarkStart

        oElement = oBookmarkStart.NextSibling

        While (Not IsNothing(oElement) And Not oElement Is oBookmarkEnd)
            oElement.Remove()
            oNextElem = oElement.NextSibling
            oElement = oNextElem
        End While

        'clean up
        oElement = Nothing
        oNextElem = Nothing

        Return oBookmarkStart
    End Function

Any help is appreciated

jim


Viewing all articles
Browse latest Browse all 1288

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>