Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9322749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T04:10:18+00:00 2026-06-19T04:10:18+00:00

The dilemma is rather simple. I need to create a small app that will

  • 0

The dilemma is rather simple. I need to create a small app that will clear all font background colors (leave table cell background colours unchanged), and remove all text with strikethrough in a word document, and then save the document into another folder. Otherwise the document’s formatting should remain untouched.

Below is a large-ish example scraped together from random examples available in google showing how to apply specific kinds of formatting to random strings found using Find.Execute(). I have no clue however, on how to only do as described above.

public static string searchDoc(string fileNameRef)
    {

        Microsoft.Office.Interop.Word._Application word = new Microsoft.Office.Interop.Word.Application(); ;
        Microsoft.Office.Interop.Word._Document doc = new Microsoft.Office.Interop.Word.Document();
        object missing = System.Type.Missing;

        try
        {
            System.IO.FileInfo ExecutableFileInfo =
                    new System.IO.FileInfo(System.Reflection.Assembly.GetEntryAssembly().Location);

            object fileName =
                System.IO.Path.Combine(ExecutableFileInfo.DirectoryName, fileNameRef);

            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);
            doc.Activate();

            //object findStr = "hello"; //sonething to find
            // THIS is the part where I fail, I can't find of a way to Find.Execute on formatting
            // as opposed to mere strings.
            //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
            //}

            object saveFileName = ExecutableFileInfo.DirectoryName + "\\New\\" + fileNameRef;

            doc.SaveAs(ref saveFileName, 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);

        }
        catch (Exception)
        {
        }
        finally
        {
            doc.Close(ref missing, ref missing, ref missing);
            word.Application.Quit(ref missing, ref missing, ref missing);
        }

        return fileNameRef;
    }

Thanks for any help! And I do mean any, simply getting started on how to spot formatting would help a great deal, I imagine. 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T04:10:20+00:00Added an answer on June 19, 2026 at 4:10 am

    This is not a C#-specific question; it’s a Word Object Model question (I refer you to here and here).

    As to your specific question, I suggest you turn on the Macro Recorder in Word, perform the actions, and see the generated VBA code. Then you can apply it in C#.

    Try this:

    using System;
    using Microsoft.Office.Interop.Word;
    using System.IO;
    using System.Reflection;
    
    namespace WordFormattingFindReplace {
        class Program {
            static void Main(string[] args) {
            }
    
            public static string searchDoc(string fileName) {
                _Application word = new Application(); ;
                _Document doc;
    
                string folderName = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
                string filePath = Path.Combine(folderName,fileName);
    
                doc = word.Documents.Open(filePath);
    
                var find=doc.Range().Find;
                find.Text="Hello";
                find.Format=true;
                find.Replacement.Font.Name="Tahoma";
                find.Replacement.Font.ColorIndex=WdColorIndex.wdRed;
                find.Execute(Replace:WdReplace.wdReplaceAll);
    
                doc.SaveAs2(Path.Combine(folderName,"New",fileName));
    
                doc.Close();
    
                //We need to cast this to _Application to resolve which Quit method is being called
                ((_Application)word.Application).Quit();
    
                return fileName;
            }
        }
    }
    

    Some notes:

    • Use using statements for clarity. Instead of Microsoft.Office.Interop.Word._Application word, add using Microsoft.Office.Interop.Word at the top of your file, and you can then just write _Application word
    • If all you need is the folder name, use the static Path.GetDirectoryName method and save as a string variable, instead of creating a FileInfo object
    • As of .NET 4, you can skip optional arguments when calling Documents.Open, Document.SaveAs and Document.Close. This also means you don’t need an object missing.
    • There’s nothing here the user really needs to see, so calling Document.Activate is unnecessary
    • It’s probably better to reuse the Word.Application instance, instead of recreating it for each call.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Rather than use a full-blown PHP MVC, I'm designing one that will best-fit my
The dilemma: I have options that need to store pricing data. Each option can
My dilemma: I'm passing my function a string that I need to then perform
Here's the dilemma: I want to create a custom editingAccessoryView that contains two buttons
The dilemma I'm using a before_filter in my controller that restricts access to admins.
I have a dilemma that I've encountered before. What's the best in terms of
I'm facing a dilemma (!). In a first scenario, I implemented a solution that
I got a rather big class library that contains a lot of code. I
Here is my dilema, I have a rather large PHP script, that basically reads
Interesting dilemma here. I need the ability for users to add links from their

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.