none
How to save and restore different text styles (Quick Style) inside one paragraph in MS Word RRS feed

  • Question

  • I have some text that contains, say 10 words, and several words in this text have specific styles, e.g. one word is bold and another one is red.

    How can I save these styles so that after part of the text is changed I could restore them?

    1) I already tried to save and restore style of entire range but it does not work

    var style = Range.get_Style() // does not keep style for single word inside range

    Range.Text = range.Text + " some extra word"; // this breaks all previous formatting

    Range.set_Style(style) // this does not restore existed styles

    2) Now I am looping through Range.Characters, check property QuickStyle for each one and if it is true then I save / restore style for specific character. My main concern here is performance because if text is huge then its processing may take some time which is not good for users.

    Is there a faster or easier way to keep text styles in consistence before and after changes?

    Example :

    I need to go for a walk [and here may go some red text].




    • Edited by arteinvolo Monday, December 22, 2014 5:42 PM
    Monday, December 22, 2014 5:17 PM

Answers

  • Hi Arteinvolo,

    I probably don't fully understand what you are trying to do but here are a few commands and methods that you might consider using in your solution.

    Unlike the Range.Text command, Range.FormattedText will retain the formatting.

    When looking for formatting changes you need to look for two types, paragraph and font. Paragraph formatting would be changes such as lines spacing, points before and after, and indenting. You can check these by iterating thru the document's paragraphs and checking those against the original paragraph style in the document. Here is an example:

                'Line Spacing
                If p.Range.ParagraphFormat.LineSpacing <> doc.Styles(sStyle).ParagraphFormat.LineSpacing Then
                    'do something
                End If
                
                'Space Before
                If p.Range.ParagraphFormat.SpaceBefore <> doc.Styles(sStyle).ParagraphFormat.SpaceBefore Then
                    'do something
                End If
    

    For checking changes made to font formatting I use Track Changes and the Range.Font.Reset command. When you reset the range's font you are taking away whatever direct formatting was applied to the text and when doing so with Tracked changes turned on, you now have a collection created of revisions that are marking where and what was changed on the specific chunks of text.

    You search the revision description string for certain keywords and phrases to determine what you need to do. The command looks like: str = rng.Revisions.Item(i).FormatDescription and then you look at what is mentioned in the "str" string, just remember... because you "reset" the font formatting to the original formatting, the revision description will read the opposite of what was applied thus if the text had been italicized then the description will read "Not Italic". The "Not Italic" message will be your cue to reapply Italics to the text.

    Hope this helps


    Kind Regards, Rich ... http://greatcirclelearning.com

    Tuesday, December 23, 2014 11:02 AM
  • Hi Arteinvolo,

    Thanks for posting in MSDN forum.

    According to the description, you want to save the style and text to restore them after they are changed.

    Based on my understanding, the Word application provides Undo action. Is it suitable for the reiqurement?

    If not, I suggest that you copy the text into an template Word document and it would copy the style of text as default. And when you want to restore the text you can copy them back.

    Hope it is helpful.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, December 23, 2014 6:10 AM
    Moderator

All replies

  • Hi Arteinvolo,

    Thanks for posting in MSDN forum.

    According to the description, you want to save the style and text to restore them after they are changed.

    Based on my understanding, the Word application provides Undo action. Is it suitable for the reiqurement?

    If not, I suggest that you copy the text into an template Word document and it would copy the style of text as default. And when you want to restore the text you can copy them back.

    Hope it is helpful.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, December 23, 2014 6:10 AM
    Moderator
  • Hi Arteinvolo,

    I probably don't fully understand what you are trying to do but here are a few commands and methods that you might consider using in your solution.

    Unlike the Range.Text command, Range.FormattedText will retain the formatting.

    When looking for formatting changes you need to look for two types, paragraph and font. Paragraph formatting would be changes such as lines spacing, points before and after, and indenting. You can check these by iterating thru the document's paragraphs and checking those against the original paragraph style in the document. Here is an example:

                'Line Spacing
                If p.Range.ParagraphFormat.LineSpacing <> doc.Styles(sStyle).ParagraphFormat.LineSpacing Then
                    'do something
                End If
                
                'Space Before
                If p.Range.ParagraphFormat.SpaceBefore <> doc.Styles(sStyle).ParagraphFormat.SpaceBefore Then
                    'do something
                End If
    

    For checking changes made to font formatting I use Track Changes and the Range.Font.Reset command. When you reset the range's font you are taking away whatever direct formatting was applied to the text and when doing so with Tracked changes turned on, you now have a collection created of revisions that are marking where and what was changed on the specific chunks of text.

    You search the revision description string for certain keywords and phrases to determine what you need to do. The command looks like: str = rng.Revisions.Item(i).FormatDescription and then you look at what is mentioned in the "str" string, just remember... because you "reset" the font formatting to the original formatting, the revision description will read the opposite of what was applied thus if the text had been italicized then the description will read "Not Italic". The "Not Italic" message will be your cue to reapply Italics to the text.

    Hope this helps


    Kind Regards, Rich ... http://greatcirclelearning.com

    Tuesday, December 23, 2014 11:02 AM