?
Solved

C# Microsoft.Interop.Word Search and Replace Fonts

Posted on 2014-04-07
8
Medium Priority
?
2,584 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 total points
ID: 39983772
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

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