Solved

C# Microsoft.Interop.Word Search and Replace Fonts

Posted on 2014-04-07
8
2,448 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

828 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