none
How to preserve tablestyle when StylesDefinition from a template is applied RRS feed

  • Question

  • Hi

    In our project we are creating documents from a predefined text-document on which we apply headers, footers, margins, themes and styles from a Word template (dotx).

    The solution has worked fine as a webservice in every case so far, but we are now experiencing problems regarding table styles.

    If the predefined text has a tablestyle set, for instance "Medium dark-shadow1-color 1" (translated from swedish), which also exists in the template and is a part af the standard Office Theme, the document looses the style and the table ends up as plain white one.

    We apply styles from the template with the following code:

       Friend Sub WDCopyStyles(ByVal fromdoc As String, ByVal toDoc As String)
    
            Dim wordDoc1 As WordprocessingDocument = WordprocessingDocument.Open(fromdoc, False)
            Dim wordDoc2 As WordprocessingDocument = WordprocessingDocument.Open(toDoc, True)
    
            Dim styleDefPart = wordDoc1.MainDocumentPart.GetPartsOfType(Of StyleDefinitionsPart).FirstOrDefault()
            Dim styleDefPart2 = wordDoc2.MainDocumentPart.GetPartsOfType(Of StyleDefinitionsPart).FirstOrDefault()
            Using wordDoc2
                If styleDefPart2 Is Nothing Then
                    wordDoc2.MainDocumentPart.AddPart(Of StyleDefinitionsPart)(styleDefPart)
                Else
                    wordDoc2.MainDocumentPart.DeletePart(styleDefPart2)
                    wordDoc2.MainDocumentPart.AddPart(Of StyleDefinitionsPart)(styleDefPart)
                End If
    
            End Using
    
            wordDoc1.Close()
    
        End Sub

    What am I missing?

    Thanks in advance


    Best Regards Peter Karlström Midrange AB, Sweden

    Friday, December 6, 2013 12:38 PM

Answers

  • Hi Cindy

    Finally, I hade some more time to dig in to this issue.

    Sorry to say, your suggestion to manipulate the document by using comparer code will not work in a system that is supposed to deliver documents of a wide range of types.

    I found a way to make the tablestyle to apply to the document by changing the order in which I process the making of the document, and then use a slightly modified version of OpenXMLPowertools DocumentBuilder to merge the created document with the template.
    All styles from the template are now attached to the produced document and the tablestyle shows up correct.

    However, in predefined documents based on the Normal template, some formats looks incorrect, but adjust themselves to a correct look when a manually "ApplyStyles" is used. This we can live with for the time beeing.

    This "ApplyStyles" function would be nice to have in the Open XML SDK, don't you think?

    Thanks for your support.


    Best Regards Peter Karlström Midrange AB, Sweden

    Friday, January 24, 2014 3:07 PM

All replies

  • Hi Peter

    Well, the first thing I'd check is whether the styleId used in the document and the styleId used in the template match. Whenever I've encountered a problem like this (text doesn't reflect the style name applied to it) it's turned out to be the wrong styleId in the text. And please note that capitalization is important - it must be an exact match.

    Edit: I'm making the assumption, here, that the actual adding of the StyleDefs part is successful and all other styles are working correctly...


    Cindy Meister, VSTO/Word MVP, my blog


    Sunday, December 8, 2013 8:07 AM
    Moderator
  • Hi Cindy

    Thanks for your reply.

    I've studied the result and the template styledefinition and the spelling is exact.

    When I look at the resulting documents XML, the document.xml specifies the table style like this:

    <w:tbl>
    <w:tblPr><w:tblStyle w:val="Mellanmrkskuggning1-dekorfrg1"/>
    <w:tblW w:w="0" w:type="auto"/>
    <w:tblLook w:val="04A0" w:noVBand="1" w:noHBand="0" w:lastColumn="0" w:firstColumn="1" w:lastRow="0" w:firstRow="1"/>
    </w:tblPr>

    In the StylesWithEffects XMLfile the specification for this is:

    <w:style w:styleId="Mellanmrkskuggning1-dekorfrg1" w:type="table">
    	<w:name w:val="Medium Shading 1 Accent 1"/>
    	<w:basedOn w:val="Normaltabell"/>
    	<w:uiPriority w:val="63"/>
    	<w:rsid w:val="00EE7326"/>
    	<w:pPr>
    		<w:spacing w:lineRule="auto" w:line="240" w:after="0"/>
    	</w:pPr>

    In my opinion this should result in the table having this style applied, but the table is white without the style.

    Please let me know If you want to have a look at the document itself.


    Best Regards Peter Karlström Midrange AB, Sweden

    Tuesday, December 10, 2013 9:41 AM
  • Are you sure the table style definition belongs in StylesWithEffects.xml? Shouldn't it be in styles.xml?

    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, December 12, 2013 5:32 PM
    Moderator
  • Hi Cindy

    Interesting question, so I checked for differences in my created document and a manually created one.

    In both documents, the style id in document.xml can be found in both stylesWithEffects.xml and in styles.xml. The specifications for the style are also exactly alike.

    The differences in the documents seems to be in how the style is used.

    In the document where the style is shown, this code is shown:

    <w:tblPr>
    <w:tblStyle w:val="Mellanmrkskuggning1-dekorfrg1"/>
    <w:tblW w:w="0" w:type="auto"/>
    <w:tblLook w:val="04A0" w:noVBand="1" w:noHBand="0" w:lastColumn="0" w:firstColumn="1" w:lastRow="0" w:firstRow="1"/>
    </w:tblPr>

    And in the auto-created document it looks like this:

    <w:tblPr><w:tblStyle w:val="Mellanmrkskuggning1-dekorfrg1"/>
    <w:tblW w:w="9168" w:type="dxa"/>
    <w:tblInd w:w="108" w:type="dxa"/>
    <w:tblLook w:val="04A0" w:noVBand="1" w:noHBand="0" w:lastColumn="0" w:firstColumn="1" w:lastRow="0" w:firstRow="1"/>
    </w:tblPr>

    I guess this has to do with how the style is applied in the document, but how can I adjust it after the attachment of the word templates styles?


    Best Regards Peter Karlström Midrange AB, Sweden

    Friday, December 13, 2013 7:40 AM
  • Hi Peter

    Unless I'm missing something, the differences are in tblW and tblInd? It's a mystery to me, what effects these should have on displaying a table style... Perhaps it's the "auto" assignment to the type attribute.

    If you manually edit the XML to duplicate the one that works, does the table then appear?

    If yes, try generating the code needed to create the one document from the other (or the desired document from your starting point) using the Open XML Productivity Tool. That should tell you which classes and properties you're missing.


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, December 13, 2013 10:18 AM
    Moderator
  • Hi George

    Please give me some more time to explore your latest reply.


    Best Regards Peter Karlström Midrange AB, Sweden

    Sunday, December 15, 2013 6:15 PM
  • Hi Cindy

    This is to George Hua:

    Please give me some more time to explore Cindys reply before marking this as an answer.


    Best Regards Peter Karlström Midrange AB, Sweden

    Sunday, December 15, 2013 6:17 PM
  • Hi Cindy

    Finally, I hade some more time to dig in to this issue.

    Sorry to say, your suggestion to manipulate the document by using comparer code will not work in a system that is supposed to deliver documents of a wide range of types.

    I found a way to make the tablestyle to apply to the document by changing the order in which I process the making of the document, and then use a slightly modified version of OpenXMLPowertools DocumentBuilder to merge the created document with the template.
    All styles from the template are now attached to the produced document and the tablestyle shows up correct.

    However, in predefined documents based on the Normal template, some formats looks incorrect, but adjust themselves to a correct look when a manually "ApplyStyles" is used. This we can live with for the time beeing.

    This "ApplyStyles" function would be nice to have in the Open XML SDK, don't you think?

    Thanks for your support.


    Best Regards Peter Karlström Midrange AB, Sweden

    Friday, January 24, 2014 3:07 PM