Solved

C# Microsoft.Interop.Word Search and Replace Fonts

Posted on 2014-04-07
8
2,483 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Running ftp server and transfering data to the path using Windows Service 6 40
Updating a single record in forEach using C#/Entity 7 26
C# XML Get Values 4 37
C# LINQ 5 27
This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
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 walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
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 …

696 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