Solved

C# Microsoft.Interop.Word Search and Replace Fonts

Posted on 2014-04-07
8
2,417 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

770 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