none
Word 2010 Add in to select words and restore styles RRS feed

  • Question

  • Hello, I'm having a bit of trouble creating an Add in that selects each word in a document and restores its previously applied style. The problem is that it will run through every word fine, but if the document contains more than 9 paragraphs, the last one will not restore the style. 

    So if I have a 9 paragraph document, they all restore fine, if I have a 10 paragraph document, the 10th paragraph will not restore, if I have a 20 paragraph document, the first 19 restore fine but the 20th will not.

    Here is the code that I am using:

    //counts all words in the document
        object IncludeFootnotesAndEndnotes = true;
        int wordCount = Globals.ThisAddIn.Application.ActiveDocument.ComputeStatistics(Word.WdStatistic.wdStatisticWords, ref IncludeFootnotesAndEndnotes);
    
        for (int i = 1; i <= wordCount; i++)
        {
    //selects each word in turn and restores its previously set style
        Word.Range rng = Globals.ThisAddIn.Application.ActiveDocument.Words[i];
        Word.Style previousStyle = rng.get_Style();
        rng.set_Style(previousStyle);
        }
    Any help would be greatly appreciated.


    • Edited by paulnbond Tuesday, September 18, 2012 10:11 AM
    Tuesday, September 18, 2012 10:03 AM

Answers

  • Hi pauInbond,

    Thanks for posting in the MSDN Forum.

    Based on your issue, I have reproduced your issue with above code. It based on my experience that the Document.ComputeStatistics(Word.WdStatistic.wdStatisticWords,true) gets a statistic of words based on the contents of the document, not including the space characters, please refer to http://msdn.microsoft.com/en-us/library/microsoft.office.tools.word.document.computestatistics(v=vs.100).aspx; However, the Document.Words property represents all the words in the document, including the space characters, please refer to http://msdn.microsoft.com/en-us/library/microsoft.office.tools.word.document.words(v=vs.100).aspx 

    Therefore, the wordCount in your code is not equal to the ActiveDocument.Words.Count. That's the reason why the styles in the last section of the document can not be restored. I would suggest you try the following code

    //counts all words in the document
        int wordCount = Globals.ThisAddIn.Application.ActiveDocument.Words.Count;
    
        for (int i = 1; i <= wordCount; i++)
        {
    //selects each word in turn and restores its previously set style
        Word.Range rng = Globals.ThisAddIn.Application.ActiveDocument.Words[i];
        Word.Style previousStyle = rng.get_Style();
        rng.set_Style(previousStyle);
        }

    Hope this will help you.

    Best Regards,


    Leo_Gao [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by paulnbond Wednesday, September 19, 2012 8:12 AM
    Wednesday, September 19, 2012 6:49 AM
    Moderator

All replies

  • Hi pauInbond,

    Thanks for posting in the MSDN Forum.

    Based on your issue, I have reproduced your issue with above code. It based on my experience that the Document.ComputeStatistics(Word.WdStatistic.wdStatisticWords,true) gets a statistic of words based on the contents of the document, not including the space characters, please refer to http://msdn.microsoft.com/en-us/library/microsoft.office.tools.word.document.computestatistics(v=vs.100).aspx; However, the Document.Words property represents all the words in the document, including the space characters, please refer to http://msdn.microsoft.com/en-us/library/microsoft.office.tools.word.document.words(v=vs.100).aspx 

    Therefore, the wordCount in your code is not equal to the ActiveDocument.Words.Count. That's the reason why the styles in the last section of the document can not be restored. I would suggest you try the following code

    //counts all words in the document
        int wordCount = Globals.ThisAddIn.Application.ActiveDocument.Words.Count;
    
        for (int i = 1; i <= wordCount; i++)
        {
    //selects each word in turn and restores its previously set style
        Word.Range rng = Globals.ThisAddIn.Application.ActiveDocument.Words[i];
        Word.Style previousStyle = rng.get_Style();
        rng.set_Style(previousStyle);
        }

    Hope this will help you.

    Best Regards,


    Leo_Gao [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by paulnbond Wednesday, September 19, 2012 8:12 AM
    Wednesday, September 19, 2012 6:49 AM
    Moderator
  • That works great thanks :) and cheers for the links, I'll make sure to read up on it.
    Wednesday, September 19, 2012 8:13 AM