Word: insert styled paragraphs using Range.InsertXML() RRS feed

  • Question

  • I've tried very different approaches to do something I'd consider a somewhat basic task in automating Word.

    I'm interested in inserting some paragraphs of text in a Word Range using InsertXML(). My XML looks similar to this example:

    <?xml version="1.0" standalone="yes"?>
    <?mso-application progid="Word.Document"?>
        <w:pStyle w:val=""Custom Existing Style""/>
         This is my first paragraph intended to use some specific style. Since I don't want to require any care about what the style exactly looks like I choose style instead of assigning font name, size, weight etc.
        <w:pStyle w:val=""Another Custom Existing Style""/>
         This is another paragraph intended to use some style different from the first one's.

    The code is indented here to simplify reading, but using no intermediate whitespace in code. Using Range.InsertXML() two new paragraphs are created, but the selected styles aren't applied as desired. The values of w:val attributes in w:pStyle elements is exactly matching NameLocal properties of existing styles in document.

    What's the problem? Is my intention supported by Word/VSTO 2010 at all?

    Using Range.get_xml() to read out existing XML of manually styled paragraphs there are some numeric references found in attributes (rsID etc.), guess that's referring to some runtime-dependent handles and thus won't persist from runtime to runtime, e.g. if saved to file.

    Currently, I'm inserting code, then enumerating over added Paragraphs in Range assigning styles in C#. But that's taking several seconds to complete on a set of about 15 paragraphs, thus performing ridiculously. Furthermore I'd like to have some XML translated to WordML fragments as demonstrated above using XSL. In a perfect world this would include character styling (e.g. like <em> in XHTML) again applying styles instead of styling text in code. And then enumerating Paragraphs isn't solution anymore.

    • Moved by Cindy Meister MVPModerator Sunday, October 2, 2011 6:53 AM OpenXML syntax required (From:Visual Studio Tools for Office)
    Saturday, October 1, 2011 5:59 PM

All replies

  • Hi Soletan

    I'm sure this should be possible, but can't give you any idea of the correct syntax. That's a fairly specialized area and probably best asked in the forums on or in the Open XML SDK forum. I'm going to move your question to the latter.

    I suspect that this might have something to do with needing to reference the style, somehow. It might help you to create a fairly small document with exactly this bit of style formatting, select it, then use: Selection.Range.WordOpenXML and see what that contains. This will be in the same FlatFile Open XML you require. You can definitely ignore the rsId stuff - that's for improving reliability when doing document Compare/Merge.

    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Soletan Sunday, October 2, 2011 7:11 AM
    • Unmarked as answer by Soletan Saturday, October 8, 2011 9:27 PM
    Sunday, October 2, 2011 6:53 AM
  • Your syntax is correct (except for the double double quotes) but you must use Style Ids and not style names.  If you look at any range in the document's .WordOpenXML you'll be able to get the styles.xml out of the package and find the Ids.  Note that in the xml you insert you must have your own styles.xml (you can reuse the existing one you just pulled from the document) and your style Id must reference a style from the styles.xml you're providing.
    • Edited by Mikey Jensen Thursday, December 15, 2011 7:16 PM
    Thursday, December 15, 2011 7:05 PM