none
How to identify all the paragraphs which does not have style RRS feed

  • Question

  • Hi,

    I want to apply a custom style to all the paragraphs which does not have any style applied to it yet. If I write code to identify it as

    foreach (Word.Paragraph _para in GlobalObjects.PublishWordDocument.Paragraphs)
    {
    if (_para.Range.get_Style() == null)<br/>  _paraStyleName = string.Empty;<br/>else<br/>  _paraStyleName = ((Word.Style)_para.Range.get_Style()).NameLocal;<br/><br/>if (_paraStyleName == string.Empty || _paraStyleName == "Normal (Web)" || _paraStyleName == "Normal")
    {
    _para.Range.set_Style(ref normalStyle);
    }
    }
    

    This is taking so much time to execute on a bigger document, but it works cleaner. When I tried the below approach, I could able to set custom style to all "Normal" styled paragraphs but not to "Normal (Web)" and no style paragraphs.

    Word.Find _find = GlobalObjects.PublishWordDocument.Range(ref GlobalObjects.oMissing, ref GlobalObjects.oMissing).Find;
    _find.ClearFormatting();
    _find.set_Style("Normal (Web)");
    _find.Format = true;
    _find.Execute();
    GlobalObjects.PublishWordDocument.Application.Run("SelectSimilarFormatting");
    GlobalObjects.PublishWordDocument.Application.Selection.set_Style(normalStyle);
    

    Can any one suggest me how to do this quicker and cleaner?


    Thanks a lot. Sree
    Friday, August 26, 2011 2:27 PM

Answers

  • Hi Sree

    Well, it might help that it's not possible to have text in a Word document with "no style". Text must have a style, by default that will be Normal. So taking out that comparison could help speed things up somewhat (although not significantly, perhaps).

    That would also explain why your second approach won't work for "no style", because such a thing simply is not allowed by Word.

    When you say the second does not work with "Normal (Web)" could you please be more specific about HOW it doesn't work?

    It's also not necessary to use the last two lines of code, selecting the paragraphs. Once you get Find to work with a specific style you should be able to use Find.Replacement.set_Style to replace the found style with a different one.

    A note, although it might not help you: If you need to remove "foreign" styles from a document, all you need to do is use the Delete method, something like (VB-speak): ActiveDocument.Styles("Standard (Web)").Delete()


    Cindy Meister, VSTO/Word MVP
    Friday, August 26, 2011 4:22 PM
    Moderator
  • Adding to Cindy's response, a Find command looking for a style will not locate an empty Paragraph mark in a document unless you add a ".text" property with a value of "^p" to the the search criteria.


    Kind Regards, Rich ... http://greatcirclelearning.com
    Friday, August 26, 2011 4:56 PM

All replies

  • Hi Sree

    Well, it might help that it's not possible to have text in a Word document with "no style". Text must have a style, by default that will be Normal. So taking out that comparison could help speed things up somewhat (although not significantly, perhaps).

    That would also explain why your second approach won't work for "no style", because such a thing simply is not allowed by Word.

    When you say the second does not work with "Normal (Web)" could you please be more specific about HOW it doesn't work?

    It's also not necessary to use the last two lines of code, selecting the paragraphs. Once you get Find to work with a specific style you should be able to use Find.Replacement.set_Style to replace the found style with a different one.

    A note, although it might not help you: If you need to remove "foreign" styles from a document, all you need to do is use the Delete method, something like (VB-speak): ActiveDocument.Styles("Standard (Web)").Delete()


    Cindy Meister, VSTO/Word MVP
    Friday, August 26, 2011 4:22 PM
    Moderator
  • Adding to Cindy's response, a Find command looking for a style will not locate an empty Paragraph mark in a document unless you add a ".text" property with a value of "^p" to the the search criteria.


    Kind Regards, Rich ... http://greatcirclelearning.com
    Friday, August 26, 2011 4:56 PM
  • Thanks Cindy and Rich. It was my misunderstanding about the Find.Execute(). Fid.Execute with replace option solves the issue. Thanks for your help.
    Thanks a lot. Sree
    Monday, August 29, 2011 1:41 PM