none
Why is my style of my range getting changed when I do an insert operation on the range text RRS feed

  • Question

  • If I do an insert into a range like so: Range line = document.Sentence[0];

    line.Text.Insert(indexOfInsertPosition, wordToInsert);

     

    If this executes under a previous line in a word document that has a different style the the text I'm currently working on changes to that style.

         e.g. "The rain in spain stays mainly in the plane".

         The previous line comes a movie.

    If I were to insert from into the second line this is what I get

           "The rain in spain stays mainly in the plane".

            The previous line comes from a movie.

    What is causing this and how can I fix it.

    Monday, July 11, 2011 6:31 PM

Answers

  • Hi Chrys

    The Word object model has no Insert method for the Text property. This looks like a .NET Framework method. So you aren't actually showing us how you place the result back into the Word document. Without that information it's not possible to say exactly what's going on, we can only guess. But...

    If you manipulate text "outside" the Word environment it will not retain the formatting. It's just a sequence of characters that you're placing into the document. When text is put into the Word document it must take on formatting characteristics - those of the surrounding text.

    Lupu has made a possible suggestion. Whether it's optimal it's not possible to say, based on the little information you've given us about the logic of what you need to do...


    Cindy Meister, VSTO/Word MVP
    Tuesday, July 12, 2011 6:40 AM
    Moderator

All replies

  • Hello,

     

    You can do something else to insert the text (C# example):

     

    document.Range(indexOfInsertPosition,indexOfInsertPosition).Text = wordToInsert;
    


    Best regards,

    Silviu.


    http://www.rosoftlab.net/
    Tuesday, July 12, 2011 5:49 AM
  • Hi Chrys

    The Word object model has no Insert method for the Text property. This looks like a .NET Framework method. So you aren't actually showing us how you place the result back into the Word document. Without that information it's not possible to say exactly what's going on, we can only guess. But...

    If you manipulate text "outside" the Word environment it will not retain the formatting. It's just a sequence of characters that you're placing into the document. When text is put into the Word document it must take on formatting characteristics - those of the surrounding text.

    Lupu has made a possible suggestion. Whether it's optimal it's not possible to say, based on the little information you've given us about the logic of what you need to do...


    Cindy Meister, VSTO/Word MVP
    Tuesday, July 12, 2011 6:40 AM
    Moderator