Solved

C# Microsoft.Interop.Word Search and Replace Fonts

Posted on 2014-04-07
8
2,354 Views
Last Modified: 2014-04-07
Is there a way to perform a search and replace a font in word via C#?
0
Comment
Question by:CipherIS
  • 5
  • 3
8 Comments
 
LVL 16

Expert Comment

by:Kalpesh Chhatrala
ID: 39983680
you can search text and replace that text font.

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("C:\MyDocuments\Hello.doc");
document.Activate();
object findStr = "hello"; // Find Text in Word Document
            
while (word.Selection.Find.Execute(ref findStr))  //If Found...
 {
         //change font and format of matched words
         word.Selection.Font.Name = "Arial"; //change font to Arial
}
document.Close();

Open in new window

0
 
LVL 1

Author Comment

by:CipherIS
ID: 39983704
The problem is that text changes.  I do know the length and the pattern i'm looking for.

C*******C A********A ********C

Everything in the * are numbers which are not constant and change.  The only constant is the "C" and "A's" in the string.
0
 
LVL 16

Accepted Solution

by:
Kalpesh Chhatrala earned 500 total points
ID: 39983772
0
 
LVL 1

Author Comment

by:CipherIS
ID: 39984035
how would i set below to look for my pattern?

            wApp.Selection.Find.Text = "/[0-9]*/";

Open in new window

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Comment

by:CipherIS
ID: 39984148
I wrote the below code but its setting the font to the whole document.  Any idea how to resolve?

private void ReplaceFont(Microsoft.Office.Interop.Word.Document masterDoc)
{
    masterDoc.Application.Activate();
    masterDoc.Application.Selection.Find.ClearFormatting();
    masterDoc.Application.Selection.Find.MatchWildcards = true;
    masterDoc.Application.Selection.Find.Wrap = 
           Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
    masterDoc.Application.Selection.Find.Text = "C*C A*A *C";
    masterDoc.Application.Selection.Find.MatchWildcards = true;

    bool isFound = true;
    while (isFound == true)
    {
        isFound = masterDoc.Application.Selection.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);

                if (isFound == true)
                {
                    masterDoc.Application.Selection.Font.Name = "E-13B";
                }
            }
        }

Open in new window

0
 
LVL 16

Expert Comment

by:Kalpesh Chhatrala
ID: 39984220
try like below

    masterDoc.Application.Activate();
    masterDoc.Application.Selection.Find.ClearFormatting();
    masterDoc.Application.Selection.Find.MatchWildcards = true;
    masterDoc.Application.Selection.Find.Wrap = 
           Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;

    masterDoc.Application.Selection.Find.MatchWildcards = true;

    object findStr = "C*C A*A *C";

    while (masterDoc.Selection.Find.Execute(ref findStr))  //If Found...
    {
         //change font and format of matched words
         masterDoc.Selection.Font.Name = "E-13B";
     }

Open in new window

0
 
LVL 1

Author Comment

by:CipherIS
ID: 39984251
This was the fix

masterDoc.Application.Selection.Find.Text = "<C[0-9]*C A[0-9]*A [0-9]*C>";

Open in new window


Thank You
0
 
LVL 1

Author Closing Comment

by:CipherIS
ID: 39984253
It got me to where I need to go.  Thx
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now