Home > Software design >  Microsoft Interop Find and Replace with Formatting
Microsoft Interop Find and Replace with Formatting

Time:10-06

I am making a windows form app that will take in user input and upon submission it will open a word template and find and replace date from the windows form to the word document. I would like the replaced text to be underlined. I am using a function I found from another user but I am not sure how to set up the formatting. Here is the function I am using:


private void FindAndReplace(Microsoft.Office.Interop.Word.Application wordApp, object toFindText, object replaceWithText)
{
    object matchCase = true;
    object matchwholeWord = true;
    object matchwildCards = false;
    object matchSoundLike = false;
    object nmatchAllforms = false;
    object forward = true;
    object format = false;
    object matchKashida = false;
    object matchDiactitics = false;
    object matchAlefHamza = false;
    object matchControl = false;
    object read_only = false;
    object visible = true;
    object replace = -2;
    object wrap = 1;
    wordApp.Selection.Find.Execute(ref toFindText, ref matchCase,
                                    ref matchwholeWord, ref matchwildCards, ref matchSoundLike,
                                    ref nmatchAllforms, ref forward,
                                    ref wrap, ref format, ref replaceWithText,
                                    ref replace, ref matchKashida,
                                    ref matchDiactitics, ref matchAlefHamza,
                                    ref matchControl);
}

I can see there is a object format = false, but when I check the documentation I do not see how I can set the formatting options.

CodePudding user response:

I have found a solution. I just added a line of code in the function I used above, below the line

wordApp.Selection.Find.Execute(ref toFindText, ref matchCase,
                               ref matchwholeWord, ref matchwildCards, ref matchSoundLike,
                               ref nmatchAllforms, ref forward,
                               ref wrap, ref format, ref replaceWithText,
                               ref replace, ref matchKashida,
                               ref matchDiactitics, ref matchAlefHamza,
                               ref matchControl);

I added

wordApp.Selection.Font.Underline = Microsoft.Office.Interop.Word.WdUnderline.wdUnderlineSingle;
  • Related