none
C# search and replace font color in Word RRS feed

  • Question

  • i'm struggling with what i have read on the internet and here on the forums. i need some clarity on how to find a match on a string in a 2003 Word document and using Visual Studio C# 2010 to change the string in a different font.

    it would need to scan each line and change the font for the matching string.

    can anyone help explain what is being the fog?

     

    • Moved by CoolDadTx Saturday, September 3, 2011 10:31 PM Office related (From:Visual C# General)
    Saturday, September 3, 2011 4:33 AM

Answers

  • Hi,

    Thanks for telling me that...It's my mistake, we need to change Find.Format property to true in ReplaceFont method:

            public bool ReplaceFont(Word.Range rng, string findWhat)
            {
                bool hasFound = false;
                rng.Find.ClearFormatting();
                rng.Find.Replacement.ClearFormatting();
                rng.Find.Replacement.Font.ColorIndex = Word.WdColorIndex.wdBlue;
                rng.Find.Text = findWhat;
                rng.Find.Replacement.Text = findWhat;
                rng.Find.Forward = true;
                rng.Find.Wrap = Word.WdFindWrap.wdFindStop;
    
                //change this property to true as we want to replace format
                rng.Find.Format = true;
    
                hasFound = rng.Find.Execute(Replace: Word.WdReplace.wdReplaceAll);
                return hasFound;
            }
    

    Good day :-)


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by hauld Tuesday, September 13, 2011 5:45 PM
    Tuesday, September 13, 2011 4:03 PM
    Moderator

All replies

  • Hi,

    You can use Word Automation in C# and do everything you want.

    In the following sample, Font and color of all words "hello" in a word document named test.doc is changed to Tahoma (in red).

     

                Microsoft.Office.Interop.Word._Application word;
                Microsoft.Office.Interop.Word._Document document;
                word = new Microsoft.Office.Interop.Word.Application();
                word.Visible = true;
                document = word.Documents.Open("d:\\test.doc");  //a .doc or .docx file to open
                document.Activate();
                object findStr = "hello"; //sonething to find
                
                while (word.Selection.Find.Execute(ref findStr))  //found...
                {
                    //change font and format of matched words
                    word.Selection.Font.Name = "Tahoma"; //change font to Tahoma
                    word.Selection.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdRed;  //change color to red
                }
                //document.Close();  //this closes test.doc
    
    

     

    You need to add reference to “Microsoft Word xx Object Library” from Add Reference\COM...

    For a more complete word automation sample see:  http://support.microsoft.com/kb/316384

     


    Visit my technical blog: http://csharptips.wordpress.com
    • Edited by Alan Zavari Saturday, September 3, 2011 6:51 AM
    • Proposed as answer by Jekk Microsoft Wednesday, August 9, 2017 9:51 AM
    • Unproposed as answer by Jekk Microsoft Wednesday, August 9, 2017 9:51 AM
    Saturday, September 3, 2011 6:51 AM
  • thank you Abdollah,

    could you continue to assist by providing how i can perform font color change for different search strings and different colors.

    in other words how can i do a loop to the beginning of the document and search on a multiple strings? can i put the "object findStr" to an array?

    i have 5 to 6 different search strings that will have different colors each. i thought it would be easier to reason out if i know how to do 1, i was wrong.

    Saturday, September 3, 2011 8:21 PM
  • You can simply repeat the while section as many as you wish. Just don't forget to put this line before each while.

    word.Selection.Start = 0; //clear selection
    


    This makes the Find function to search from the beginning of the document.

    The while section of your program may look like this in the end:

           //...
           word.Selection.Start = 0; 
           object findStr = "word1"; 
           while (word.Selection.Find.Execute(ref findStr)) 
            {
                    //change font and format of words WORD1...
            }
    
           word.Selection.Start = 0; 
           object findStr = "word2"; 
           while (word.Selection.Find.Execute(ref findStr))
            {
                    //change font and format of words WORD2...
            }
    
           word.Selection.Start = 0; 
           object findStr = "word3"; 
           while (word.Selection.Find.Execute(ref findStr))
            {
                    //change font and format of words WORD3...
            }
           //...
    
    
    
    
    Good luck


    Visit my technical blog: http://csharptips.wordpress.com
    Sunday, September 4, 2011 9:23 AM
  • it does not seem to repeat the loop. here is my test code i built based on samples.

             private void testWord()
            {

                // Create the Word application and declare a document
                Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
                Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();

                // Define an object to pass to the API for missing parameters
                object missing = System.Type.Missing;

                try
                {
                    // Everything that goes to the interop must be an object
                    object fileName = System.Windows.Forms.Application.StartupPath + @"\File header.doc";

                    // Open the Word document.
                    // Pass the "missing" object defined above to all optional
                    // parameters.  All parameters must be of type object,
                    // and passed by reference.
                    doc = word.Documents.Open(ref fileName,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing);

                    // Activate the document
                    doc.Activate();
                    object findStr = "   Workorder Complete          "; //sonething to find
                   
                    while (word.Selection.Find.Execute(ref findStr))  //found...
                    {
                        //change font and format of matched words
                        //word.Selection.Font.Name = "Tahoma"; //change font to Tahoma
                        word.Selection.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdBlue;  //change color to red
                    }
                    word.Selection.Start = 0;
                    object findStr2 = "     Request       "; //sonething to find
                    while (word.Selection.Find.Execute(ref findStr2))  //found...
                    {
                        //change font and format of matched words
                        //word.Selection.Font.Name = "Tahoma"; //change font to Tahoma
                        word.Selection.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdDarkBlue;  //change color to red
                    }

                    // Save the changes
                    doc.Save();

                    // Close the doc and exit the app
                    doc.Close(ref missing, ref missing, ref missing);

                    word.Application.Quit(ref missing, ref missing, ref missing);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Exception Caught: " + ex.Message); // Could be that the document is already open (/) or Word is in Memory(?)
                }
              }

    when it gets to the second search it jumps the "while" and goes to "doc.save()".

    Sunday, September 4, 2011 3:34 PM
  • Hi Hauld,

    Thanks for your post.

    If you want to replace some text or some format in document, I would recommend you to use Find.Execute method directly:

    Function ReplaceFormat(ByVal findWhat As String) As Boolean
    
        Dim hasFound As Boolean
        
        Dim doc As Document
        Set doc = ThisDocument
        
        Dim rng As Range
        Set rng = doc.Content
        
        rng.Find.ClearFormatting
        rng.Find.Replacement.ClearFormatting
        rng.Find.Replacement.Font.Color = 12611584
        With rng.Find
            .Text = findWhat
            .Replacement.Text = findWhat
            .Forward = True
            .Wrap = wdFindContinue
            .Format = True
    
        End With
        hasFound = rng.Find.Execute(Replace:=wdReplaceAll)
       ReplaceFormat = hasFound
    End Function
    
    
    
    Sub test()
    
        Dim hasFound As Boolean
        hasFound = ReplaceFormat("hello")
        MsgBox hasFound
    
    End Sub
    


    These code is written by VBA,  you can concert them to VB.NET or C# easily.

    I hope this can help you.


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, September 5, 2011 3:30 AM
    Moderator
  • I tested your code on Win XP, Office 2010. It works just fine!

    The only probable reason for not working is that the Find method could not find "     Request       ".

     


    Visit my technical blog: http://csharptips.wordpress.com
    Monday, September 5, 2011 4:03 AM
  • i open Word and did the search manually on each condition and it found all words i want to find. however the program does not seem to find them.

    i still can't find why this is the case, i had hoped that manually doing the changes i wanted would fail too.

    code is in C#.

     

    Thursday, September 8, 2011 2:50 PM
  • Please post your source code if possible...
    Visit my technical blog: http://csharptips.wordpress.com
    Thursday, September 8, 2011 3:01 PM
  • using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Office.Interop.Word;
    using System.Windows.Forms;

    namespace GVSReport
    {
        class ReadWordDoc
        {
            public void run()
            {
                testWord();
            }

            private void testWord()
            {

                // Create the Word application and declare a document
                Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
                Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();

                // Define an object to pass to the API for missing parameters
                object missing = System.Type.Missing;

                try
                {
                    // Everything that goes to the interop must be an object
                    object fileName = System.Windows.Forms.Application.StartupPath + @"\File header.doc";

                    // Open the Word document.
                    // Pass the "missing" object defined above to all optional
                    // parameters.  All parameters must be of type object,
                    // and passed by reference.
                    doc = word.Documents.Open(ref fileName,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing);

                    // Activate the document
                    doc.Activate();

                    object findStr = "   Workorder Complete          "; //sonething to find
                    while (word.Selection.Find.Execute(ref findStr))  //found...
                    {
                        //change font and format of matched words
                        //word.Selection.Font.Name = "Tahoma"; //change font to Tahoma
                        word.Selection.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdBlue;  //change color to red
                    }
                    word.Selection.Start = 0;
                    findStr = " Test  "; //sonething to find
                    while (word.Selection.Find.Execute(ref findStr))  //found...
                    {
                        //change font and format of matched words
                        //word.Selection.Font.Name = "Tahoma"; //change font to Tahoma
                        word.Selection.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdBlue;  //change color to red
                    }
                    word.Selection.Start = 0;
                    findStr = " Other Assignments  "; //sonething to find
                    while (word.Selection.Find.Execute(ref findStr))  //found...
                    {
                        //change font and format of matched words
                        //word.Selection.Font.Name = "Tahoma"; //change font to Tahoma
                        word.Selection.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdBlue;  //change color to red
                    }               
                  
                    word.Selection.Start = 0;
                    findStr = " Request   "; //sonething to find
                    while (word.Selection.Find.Execute(ref findStr))  //found...
                    {
                        //change font and format of matched words
                        //word.Selection.Font.Name = "Tahoma"; //change font to Tahoma
                        word.Selection.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdDarkBlue;  //change color to red
                    }
                    word.Selection.Start = 0;
                    findStr = " Development   "; //sonething to find
                    while (word.Selection.Find.Execute(ref findStr))  //found...
                    {
                        //change font and format of matched words
                        //word.Selection.Font.Name = "Tahoma"; //change font to Tahoma
                        word.Selection.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdViolet;  //change color to red
                    }
                    word.Selection.Start = 0;
                    findStr = " Workaround Available   "; //sonething to find
                    while (word.Selection.Find.Execute(ref findStr))  //found...
                    {
                        //change font and format of matched words
                        //word.Selection.Font.Name = "Tahoma"; //change font to Tahoma
                        word.Selection.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdTeal;  //change color to red
                    }
                    word.Selection.Start = 0;
                    findStr = " Release   "; //sonething to find
                    while (word.Selection.Find.Execute(ref findStr))  //found...
                    {
                        //change font and format of matched words
                        //word.Selection.Font.Name = "Tahoma"; //change font to Tahoma
                        word.Selection.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdTeal;  //change color to red
                    }
                    word.Selection.Start = 0;
                    findStr = "Solution   "; //sonething to find
                    while (word.Selection.Find.Execute(ref findStr))  //found...
                    {
                        //change font and format of matched words
                        //word.Selection.Font.Name = "Tahoma"; //change font to Tahoma
                        word.Selection.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdGreen;  //change color to red
                    }
                    word.Selection.Start = 0;
                    findStr = "  Complete   "; //sonething to find
                    while (word.Selection.Find.Execute(ref findStr))  //found...
                    {
                        //change font and format of matched words
                        //word.Selection.Font.Name = "Tahoma"; //change font to Tahoma
                        word.Selection.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdGreen;  //change color to red
                    }
                    word.Selection.Start = 0;
                    findStr = "  Cancelled   "; //sonething to find
                    while (word.Selection.Find.Execute(ref findStr))  //found...
                    {
                        //change font and format of matched words
                        //word.Selection.Font.Name = "Tahoma"; //change font to Tahoma
                        word.Selection.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdGreen;  //change color to red
                    }

                    // Save the changes
                    doc.Save();

                    // Close the doc and exit the app
                    doc.Close(ref missing, ref missing, ref missing);

                    word.Application.Quit(ref missing, ref missing, ref missing);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Exception Caught: " + ex.Message); // Could be that the document is already open (/) or Word is in Memory(?)
                }
      
            }

    Thursday, September 8, 2011 3:38 PM
  • Hi Hauld,

    When you are manipulating Office object automatically, you should always avoid using selection object except it is very necessary because changing selection will always decrease the process speed.

    If you prefer finding match word one by one to using replace method, you should modify your code as this style:

    Sub FindOneByOne()
    
        Dim findWhat As String
        findWhat = "hello"
    
        Dim doc As Document
        Set doc = ThisDocument
    
        Dim rng As Range
        Set rng = doc.Content
    
        rng.Find.ClearFormatting
        With rng.Find
            .Text = "hello"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindStop
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchByte = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        
        While rng.Find.Execute
            rng.Font.name = "Tahoma"
            rng.Font.bold = wdToggle
        Wend
    
    End Sub
    

     

    If you have trouble on converting VBA code to C#, I'll help you on this.
    Good day,


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, September 9, 2011 6:53 AM
    Moderator
  • hello Calvin,

    i made an attempt to use your sample. i'm not sure were i went off the rails, here is my code. it will only make one successful pass.

    i mean it will only change the font of the first match. can you help fix my code?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Office.Interop.Word;
    using System.Windows.Forms;

    namespace GVSReport
    {
        class ReadWordDoc
        {
            public void run()
            {
                testWord();
            }

            private void testWord()
            {

                // Create the Word application and declare a document
                Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
                Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();

                // Define an object to pass to the API for missing parameters
                object missing = System.Type.Missing;

                try
                {
                    // Everything that goes to the interop must be an object
                    object fileName = System.Windows.Forms.Application.StartupPath + @"\File header.doc";

                    // Open the Word document.
                    // Pass the "missing" object defined above to all optional
                    // parameters.  All parameters must be of type object,
                    // and passed by reference.
                    doc = word.Documents.Open(ref fileName,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing);

                    // Activate the document
                    doc.Activate();

                    Microsoft.Office.Interop.Word.Range rng = doc.Content;
                    rng.Find.ClearFormatting();

                    rng.Find.Text = "   Workorder Complete";
                    while (rng.Find.Execute(ref missing,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing))
                    {
                        rng.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdBlue;  //change color to blue
                    }
                    word.Selection.Start = 0;
                    rng.Find.ClearFormatting();
                    rng.Find.Text = "   Test";
                    while (rng.Find.Execute(ref missing,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing))
                    {
                        rng.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdBlue;  //change color to blue
                    }
                    word.Selection.Start = 0;
                    rng.Find.ClearFormatting();
                    rng.Find.Text = "   Other Assignments";
                    while (rng.Find.Execute(ref missing,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing))
                    {
                        rng.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdBlue;  //change color to blue
                    }
                    word.Selection.Start = 0;
                    rng.Find.ClearFormatting();
                    rng.Find.Text = "   Request";
                    while (rng.Find.Execute(ref missing,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing))
                    {
                        rng.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdDarkBlue;  //change color to blue
                    }
                    word.Selection.Start = 0;
                    rng.Find.ClearFormatting();
                    rng.Find.Text = "   Development";
                    while (rng.Find.Execute(ref missing,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing))
                    {
                        rng.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdViolet;  //change color to blue
                    }
                    word.Selection.Start = 0;
                    rng.Find.ClearFormatting();
                    rng.Find.Text = "   Workaround Available";
                    while (rng.Find.Execute(ref missing,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing))
                    {
                        rng.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdTeal;  //change color to blue
                    }
                    word.Selection.Start = 0;
                    rng.Find.ClearFormatting();
                    rng.Find.Text = "   Release";
                    while (rng.Find.Execute(ref missing,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing))
                    {
                        rng.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdTeal;  //change color to blue
                    }
                    word.Selection.Start = 0;
                    rng.Find.ClearFormatting();
                    rng.Find.Text = "   Solution";
                    while (rng.Find.Execute(ref missing,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing))
                    {
                        rng.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdGreen;  //change color to blue
                    }
                    word.Selection.Start = 0;
                    rng.Find.ClearFormatting();
                    rng.Find.Text = "   Complete";
                    while (rng.Find.Execute(ref missing,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing))
                    {
                        rng.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdGreen;  //change color to blue
                    }
                    word.Selection.Start = 0;
                    rng.Find.ClearFormatting();
                    rng.Find.Text = "   Cancelled";
                    while (rng.Find.Execute(ref missing,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing))
                    {
                        rng.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdGreen;  //change color to blue
                    }

                    // Save the changes
                    doc.Save();

                    // Close the doc and exit the app
                    doc.Close(ref missing, ref missing, ref missing);

                    word.Application.Quit(ref missing, ref missing, ref missing);
                    MessageBox.Show("DONE");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Exception Caught: " + ex.Message); // Could be that the document is already open (/) or Word is in Memory(?)
                }
      
            }

    Friday, September 9, 2011 5:11 PM
  • Hauld,

    I copied and pasted your code to a C# project and also created a word document.

    I repeated findStr (s) in various places of the word document and ran the program. It worked just fine!

    Here is snapshot of the word document before and after running the program.

    BEFORE:

    ----------------------------------------------------------------------------------------------------------

    AFTER:


    Visit my technical blog: http://csharptips.wordpress.com
    Saturday, September 10, 2011 4:19 AM
  • i hate this! troubleshooting issues where it works on your system and not on another.

    Abdollah, thanks i will try to troubleshoot my issue.

    Monday, September 12, 2011 3:23 PM
  • when i step through the code it seems that it won't find a match on the additional search. but if i go into WORD and do a search it will.

    ? could it be it is not going to the top of the WORD?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Office.Interop.Word;
    using System.Windows.Forms;

    namespace GVSReport
    {
        class ReadWordDoc
        {
            public void run()
            {
                testWord();
            }

            private void testWord()
            {

                // Create the Word application and declare a document
                Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
                Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();

                // Define an object to pass to the API for missing parameters
                object missing = System.Type.Missing;

                try
                {
                    // Everything that goes to the interop must be an object
                    object fileName = System.Windows.Forms.Application.StartupPath + @"\File header.doc";

                    // Open the Word document.
                    // Pass the "missing" object defined above to all optional
                    // parameters.  All parameters must be of type object,
                    // and passed by reference.
                    doc = word.Documents.Open(ref fileName,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing);

                    // Activate the document
                    doc.Activate();

                    object findStr = "   Workorder Complete"; //sonething to find
                    while (word.Selection.Find.Execute(ref findStr))  //found...
                    {
                        //change font and format of matched words
                        //word.Selection.Font.Name = "Tahoma"; //change font to Tahoma
                        word.Selection.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdBlue;  //change color to red
                    }
                    word.Selection.Start = 0;
                    findStr = " Test  "; //sonething to find
                    while (word.Selection.Find.Execute(ref findStr))  //found...
                    {
                        //change font and format of matched words
                        //word.Selection.Font.Name = "Tahoma"; //change font to Tahoma
                        word.Selection.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdBlue;  //change color to red
                    }
                    word.Selection.Start = 0;
                    findStr = "   Other Assignments"; //sonething to find
                    while (word.Selection.Find.Execute(ref findStr))  //found...
                    {
                        //change font and format of matched words
                        //word.Selection.Font.Name = "Tahoma"; //change font to Tahoma
                        word.Selection.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdBlue;  //change color to red
                    }               
                  
                    word.Selection.Start = 0;
                    findStr = "   Request"; //sonething to find
                    while (word.Selection.Find.Execute(ref findStr))  //found...
                    {
                        //change font and format of matched words
                        //word.Selection.Font.Name = "Tahoma"; //change font to Tahoma
                        word.Selection.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdDarkBlue;  //change color to red
                    }
                    word.Selection.Start = 0;
                    findStr = "   Development"; //sonething to find
                    while (word.Selection.Find.Execute(ref findStr))  //found...
                    {
                        //change font and format of matched words
                        //word.Selection.Font.Name = "Tahoma"; //change font to Tahoma
                        word.Selection.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdViolet;  //change color to red
                    }
                    word.Selection.Start = 0;
                    findStr = "   Workaround Available"; //sonething to find
                    while (word.Selection.Find.Execute(ref findStr))  //found...
                    {
                        //change font and format of matched words
                        //word.Selection.Font.Name = "Tahoma"; //change font to Tahoma
                        word.Selection.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdTeal;  //change color to red
                    }
                    word.Selection.Start = 0;
                    findStr = "   Release"; //sonething to find
                    while (word.Selection.Find.Execute(ref findStr))  //found...
                    {
                        //change font and format of matched words
                        //word.Selection.Font.Name = "Tahoma"; //change font to Tahoma
                        word.Selection.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdTeal;  //change color to red
                    }
                    word.Selection.Start = 0;
                    findStr = "   Solution"; //sonething to find
                    while (word.Selection.Find.Execute(ref findStr))  //found...
                    {
                        //change font and format of matched words
                        //word.Selection.Font.Name = "Tahoma"; //change font to Tahoma
                        word.Selection.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdGreen;  //change color to red
                    }
                    word.Selection.Start = 0;
                    findStr = "   Complete"; //sonething to find
                    while (word.Selection.Find.Execute(ref findStr))  //found...
                    {
                        //change font and format of matched words
                        //word.Selection.Font.Name = "Tahoma"; //change font to Tahoma
                        word.Selection.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdGreen;  //change color to red
                    }
                    word.Selection.Start = 0;
                    findStr = "   Cancelled"; //sonething to find
                    while (word.Selection.Find.Execute(ref findStr))  //found...
                    {
                        //change font and format of matched words
                        //word.Selection.Font.Name = "Tahoma"; //change font to Tahoma
                        word.Selection.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdGreen;  //change color to red
                    }

                    // Save the changes
                    doc.Save();

                    // Close the doc and exit the app
                    doc.Close(ref missing, ref missing, ref missing);

                    word.Application.Quit(ref missing, ref missing, ref missing);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Exception Caught: " + ex.Message); // Could be that the document is already open (/) or Word is in Memory(?)
                }
      
            }

     

            //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        }//class ReadWordDoc
    }//namespace GVSReport

    Monday, September 12, 2011 4:36 PM
  • I don't think so.

    Why don't you delete extra spaces in your search expressions?  like "Request" instead of  "   Request".


    Visit my technical blog: http://csharptips.wordpress.com
    Monday, September 12, 2011 8:08 PM
  • there are many cases where "Request" will appear in the word document. i'm trying to only find a specific location where the word "release" appears. in the document the words i'm matching on are located on a specific location in a line.

    i found that with-in WORD itself there is no issue. but only when i attempt this with my C# code.

    is there a way to tell the "word.Selection.Find.Execute(ref findStr)" to be explicit? it is almost as if the "   " + "Release" is odd in the C# code.

    Monday, September 12, 2011 9:42 PM
  • Hi,

    I have written a sample project for you, you can download from my skydrive in this link:

    https://skydrive.live.com/?cid=53252f8e9d778685&sc=documents&uc=1&id=53252F8E9D778685%21153#

    Good day,


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, September 13, 2011 6:55 AM
    Moderator
  • hello Calvin,

    i want to thank you for your sample i modified it as needed to fit my Word document.

    i found that if i use button1 it does nothing. button2 appears to find the match and fix those words that my code did not.

    i'm curious, do you have any idea why "button1" would not work?

    i'm have OFFICE 2003 by the way. i try to have all the updates as well.

    Tuesday, September 13, 2011 3:33 PM

  • i'm have OFFICE 2003 by the way. i try to have all the updates as well.

    All my tests were done in Windows XP using MS Office 2010.
    Visit my technical blog: http://csharptips.wordpress.com
    Tuesday, September 13, 2011 3:47 PM
  • Hi,

    Thanks for telling me that...It's my mistake, we need to change Find.Format property to true in ReplaceFont method:

            public bool ReplaceFont(Word.Range rng, string findWhat)
            {
                bool hasFound = false;
                rng.Find.ClearFormatting();
                rng.Find.Replacement.ClearFormatting();
                rng.Find.Replacement.Font.ColorIndex = Word.WdColorIndex.wdBlue;
                rng.Find.Text = findWhat;
                rng.Find.Replacement.Text = findWhat;
                rng.Find.Forward = true;
                rng.Find.Wrap = Word.WdFindWrap.wdFindStop;
    
                //change this property to true as we want to replace format
                rng.Find.Format = true;
    
                hasFound = rng.Find.Execute(Replace: Word.WdReplace.wdReplaceAll);
                return hasFound;
            }
    

    Good day :-)


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by hauld Tuesday, September 13, 2011 5:45 PM
    Tuesday, September 13, 2011 4:03 PM
    Moderator
  • Calvin,

    "button1" works now. thanks.

    this is a good start.

    Tuesday, September 13, 2011 5:42 PM
  • Thanks for the Selection Start = 0 info. I was searching text and could not figure out why is was not working.

    word.Selection.Start = 0; //clear selection

    Sunday, July 19, 2020 7:56 PM