none
maintain the existing text format in office word use code RRS feed

  • Question

  • I tried to insert text into a word office footer without deleting the existing words, the problem is:

    - format a word that had previously been lost as the alignment is change to default
    whereas the previous is right 
    -
    if the document is page number it will be transformed into ordinary words also change the format

    how to maintain the previously existing word format?

    Microsoft.Office.Interop.Word.Range Doc = Globals.ThisAddIn.Application.Selection.Range;

    Microsoft.Office.Interop.Word.Range range1 = Doc.Sections[1].Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;

                if (range1.Text.Trim() == "")
                {
                    Doc.Sections[1].Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.FormattedText.Text = "Chapter 1";
                }
                else
                {
                    try
                    {   
                        string str = range1.Text;
                        Doc.InsertAfter(str);
                        int index = str.IndexOf("Chapter");
                        index = str.IndexOf("Chapter", index);
                        string result = str.Substring(0, index);
                        Doc.Sections[1].Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.FormattedText.Text = result + "Chapter 1";
                    }
                    catch { }
                }

     

    Thank's

    Friday, April 8, 2011 6:21 AM

Answers

  • Hi Yan

    In that case, you should use the Range.Find.Execute method and set the ReplaceWith argument to a zero-length string ""


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Yan2010 Saturday, April 9, 2011 6:22 PM
    Friday, April 8, 2011 10:28 AM
    Moderator

All replies

  • Hi Yan

    I'm not certain I'm following all the details of your problem, but let's see if the following thoughts help:

    1. A string in your code cannot carry formatting. So as soon as you assign existing content to a string, the formatting will be lost. It doesn't matter whether you use the FormattedText property in your code or not. The simple fact is, a string cannot contain Word formatting symbols.

    2. So what you'll need to do is decide where any text you want to add should be inserted within an exsiting range. From what I can tell, you'd like to insert text after the existing text.

    There are a couple of ways to do this, but one simple possibility is to use the Range.InsertAfter method. You do have this in your code, but you're not using it quite the right way to achieve the required result. Try something like this:

    Doc.InsertAfter(" + my new text");

    Does that get you any closer to the result you want to have?


    Cindy Meister, VSTO/Word MVP
    Friday, April 8, 2011 7:04 AM
    Moderator
  • Hi Cindy,

    I tried using the code Doc.InsertAfter(" + my new text") , but when I enter it more than once then the word will repeat .

    my code:

    private void button1_Click(object sender, EventArgs e)
    {

    Microsoft.Office.Interop.Word.Range Doc = Globals.ThisAddIn.Application.Selection.Range;

    Microsoft.Office.Interop.Word.Range range1 = Doc.Sections[1].Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;

                if (range1.Text.Trim() == "")
                {
                    Doc.Sections[1].Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.FormattedText.Text = "Chapter 1";
                }
                else
                {
                    try
                    {   
                        range1.InsertAfter(" - Chapter");
                    }
                    catch { }
                }

    }

    the result:

    number one - Chapter -Chapter

     

    i want text " -Chapter" appears only once, even though I click the button repeatedly

     

    Friday, April 8, 2011 7:41 AM
  • Hi Yan

    So test whether the word is already in the string?

    if (!Doc.Text.Contains("Chapter"))


    Cindy Meister, VSTO/Word MVP
    Friday, April 8, 2011 9:16 AM
    Moderator
  • Hi Cindy,

    thank's for

    what if eg range1.text is contain "[page number]

                                                    sample book

                                                    Chapter"

    how to change the contens of range1 be "[page number]

                                                               sample book"

    so the chapter word is deleted without changing the existing format in another word

     

    Friday, April 8, 2011 10:22 AM
  • Hi Yan

    In that case, you should use the Range.Find.Execute method and set the ReplaceWith argument to a zero-length string ""


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Yan2010 Saturday, April 9, 2011 6:22 PM
    Friday, April 8, 2011 10:28 AM
    Moderator