none
Get all ranges in a Word Doc that have a certain style RRS feed

  • Question

  • I've seen similar threads to this question but haven't had any success with the accepted answers.

    I'm trying to get all ranges that have a style associated with it.  The following code works for the most part but doesn't get into list paragraphs and other objects.  I really need something that enumerates the entire doc.  I need the ranges because I also need to know the start and end of each sentence/word(s) with the style.

    This is what I have which does only part of the job:

                List<Paragraph> chapters = current.Paragraphs.OfType<Paragraph>()
                            .Where(p => p.get_Style() != null && ((Style)p.get_Style()).NameLocal.Contains(style1)).ToList();

    I also tried this but it only returns an array of strings and seems to skip over lists:

       Array headings = (Array)current.GetCrossReferenceItems(WdReferenceType.wdRefTypeHeading);
                  

    I've tried the Find.Execute but that didn't work for me either (since deleted code so can't provide now).

    Anyone have any ideas?
    Thank you for your help in advance!

    Nick


    Thank you,

    Nick Metnik

    Please mark my response as helpful if it has helped you in any way or as the answer if it is a valid solution.
    Blog
    LinkedIn

    Friday, July 19, 2013 9:35 PM

Answers

  • Hi Nick

    I don't understand exactly what it is you need to do, which makes providing an answer difficult. I've never tried using LINQ in combination with the Word object model, so I'm not following your code samples very well. You are trying to work with the object model and not the Open XML SDK, correct?

    Basically, if you want to "hit" every Range formatted with a given style, then you do need to use Range.Find.Execute - in a loop so that you can process each "hit". Find will pick up the exact Range, even if only a single character has been formatted with the style.

    In order to see this, test to get the right combination and finally to get the basic syntax required, it's best to work in the Word UI with the Find dialog box. What you'll want to do is display all the options ("More" button). Click in the Find text box, but don't type anything. Click the Format button (bottom left) and choose Style. Select the style name, which will appear just below the Find text box. Now click the Find button and it should take you from instance to instance.

    Once you're getting the result you want, record executing the Find in a macro to get the basic syntax (properties) you can then transfer to your C# code.

    Note that, as long as you're working with the object model, there is no alternative to "looping" the document. There's no way to get a List or collection or array in a single step. You can add each "hit" to such a thing, but you have to "walk" those ranges. It's a different story if you work with the Open XML SDK.


    Cindy Meister, VSTO/Word MVP, my blog

    Saturday, July 20, 2013 10:31 AM
    Moderator

All replies

  • Hi Nick

    I don't understand exactly what it is you need to do, which makes providing an answer difficult. I've never tried using LINQ in combination with the Word object model, so I'm not following your code samples very well. You are trying to work with the object model and not the Open XML SDK, correct?

    Basically, if you want to "hit" every Range formatted with a given style, then you do need to use Range.Find.Execute - in a loop so that you can process each "hit". Find will pick up the exact Range, even if only a single character has been formatted with the style.

    In order to see this, test to get the right combination and finally to get the basic syntax required, it's best to work in the Word UI with the Find dialog box. What you'll want to do is display all the options ("More" button). Click in the Find text box, but don't type anything. Click the Format button (bottom left) and choose Style. Select the style name, which will appear just below the Find text box. Now click the Find button and it should take you from instance to instance.

    Once you're getting the result you want, record executing the Find in a macro to get the basic syntax (properties) you can then transfer to your C# code.

    Note that, as long as you're working with the object model, there is no alternative to "looping" the document. There's no way to get a List or collection or array in a single step. You can add each "hit" to such a thing, but you have to "walk" those ranges. It's a different story if you work with the Open XML SDK.


    Cindy Meister, VSTO/Word MVP, my blog

    Saturday, July 20, 2013 10:31 AM
    Moderator
  • Hello Cindy,

    Thank you for your help.  Does Word somehow allow you to do a style contains or style is like?
    My problem with the search is I need to find all styles that contain and variation with the name "Chapter Sections".  Right now the document has the following styles that qualify as a range that needs to be grabbed:

    1. Chapter Sections
    2. Heading 3 Char,Chapter Section Char
    3. Heading 3,Chapter Section

    It seems that Word is taking a style and creating variations of it.  One could argue that if I know there is 3 variations just repeat the find and combine the results but I don't know if I can trust that Word will not spawn another variation like "List,Heading 3, Chapter Section" or something like that.

    So in short is there a way to do the search on a style like so:  Find all ranges with the style "%Chapter Section%"?

    What I'm trying to do is roll my own Table of Contents.  It's on a Military project and their formatting is so specific that fighting Word seemed to be an uphill battle with no end in sight.

    Thanks again for your help!


    Thank you,

    Nick Metnik

    Please mark my response as helpful if it has helped you in any way or as the answer if it is a valid solution.
    Blog
    LinkedIn

    Saturday, July 20, 2013 7:51 PM
  • Hi Nick

    What you appear to be fighting with is extending a built-in style with an additional name that is more meaningful to the environment in which it's being used. Word can do certain things with its built-in styles (especially heading styles) that it can't do with other styles. So Word allows the user to extend the names of styles with additional names.

    Chapter Sections is a style that stands on its own. As far as I can see, it has no relationship to the other styles you list.

    The central object of the other styles is Heading 3. The style name "Chapter Section" has been added to it. No matter whether the user chooses Heading 3 or Chapter Section, the same style should be applied to the text. You can determine whether a style has been extended in this manner by querying the NameLocal property of the style. If the string that's returned contains one or more semicolons ( ; ) then every term that follows a semicolon is an additional name for the same style.

    As long as you're using Word's built-in Find, it doesn't matter whether you use any of the names, or the string returned by NameLocal. Find behaves just the same and locates each instance.

    Note that Heading 3;Chapter Section is a paragraph style. So Find will locate only paragraphs, not individual characters. And I imagine this is correct behavior for building a TOC, but I may be wrong about that in your special instance...

    The "Char" styles are something Word generates automatically when a style is linked. This automatically generated style is a character  style. It's purpose is to allow the user to apply the same font formatting as the paragraph style to individual characters. Most professional document writers abhor this automatic behavior that was introduced in Word some ten years ago, but it appears to be here to stay... In some versions of Word the "Char" styles are very visible; in others they tend to be hidden from the user, although present in the document. In more recent versions of Word it's possible to disable the use of Linked styles.

    In order to determine whether a paragraph style is linked to a character style, use the Linked property. In order to find out which style(s) are linked, use the LinkedStyle property.

    When using Find, Char styles must be searched in a separate action as they are a different style.

    The answer to your question: "Does Word somehow allow you to do a style contains or style is like?" is NO, Word does not support searching styles in this manner.

    Difficult for me to judge with the amount if information I have, but I do wonder whether working with Word's Open XML file format might be better for you? Are you generating these TOCs as part of the "user experience" or more or less independently of the user?

    Your only purpose is to generate a TOC, not do anything with the formatted text?

    Edit: Additional thought - if you are interacting with the user, would it make sense to first generate a Word TOC, turn that into plain text, then manipulate that?


    Cindy Meister, VSTO/Word MVP, my blog


    Sunday, July 21, 2013 2:10 PM
    Moderator
  • Hello Cindy,

    Thank you for the thorough explanation.  If I use Word's TOC it picks everything up but the Military's format is far too complex to explain in this thread, the formatting instructions are over 5 pages long.

    I think what I'll do is loop through the styles and search on every style that contains my style name.  I'll mark your answer since it really got me most of the way.  I'm interested in the Open XML SDK approach since there was a snippet online that looked promising.


    Thank you,

    Nick Metnik

    Please mark my response as helpful if it has helped you in any way or as the answer if it is a valid solution.
    Blog
    LinkedIn

    Monday, July 22, 2013 12:05 AM