0

Can anyone tell me what the equivalent of the VBA for Selection.Find.Font.Color and Selection.Find.Replacement.Font.Color are for C#??

VBA:

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Font.Color = wdColorAutomatic
Selection.Find.Replacement.Font.Color = wdColorBlue

C#:

using Microsoft.Office.Interop.Word;
using Word = Microsoft.Office.Interop.Word;

var filename = "c:\\test\\test.doc";
Word.Application wordApp = new Word.Application { Visible = false };
Word.Document doc = wordApp.Documents.Open(ref filename, ReadOnly: false, Visible: false);
Word.Find fnd = wordApp.ActiveWindow.Selection.Find;
fnd.ClearFormatting();
fnd.Replacement.ClearFormatting();
fnd.Font.ColorIndex
fnd.Forward = true;
fnd.Wrap = Word.WdFindWrap.wdFindContinue;
fnd.Format = true;
fnd.MatchCase = false;
fnd.MatchWholeWord = false;
fnd.MatchWildcards = false;
fnd.MatchSoundsLike = false;
fnd.MatchAllWordForms = false;
fnd.Font.Color?= ??? <----------------------HERE
fnd.Font.Replacement.Font.Color? = ??? <-----HERE
fnd.Text = "Find";
fnd.Replacement.Text = "Replace";
fnd.Execute(Replace: Word.WdReplace.wdReplaceAll);

In VBA it works perfectly fine but I can't figure out the C# equivalent... FYI - The first one sets the color I want to search for and the second sets the replacement text's color.

MysticRyuujin
  • 25
  • 1
  • 8
  • Use Object Browser to find stuff like this. Just type "colorblue" in the search box and you'll easily find WdColor.wdColorBlue – Hans Passant Jun 02 '14 at 08:10
  • Selection.Find.Font does not have a .Color nor does Selection.Find.Replacement.Font so what would you have me do with wdColor.wdColorBlue? In other words "fnd.Font.Color" isn't valid. – MysticRyuujin Jun 02 '14 at 12:15

1 Answers1

0

I figured it out the solution is:

fnd.Font.ColorIndex = Word.WdColorIndex.wdAuto;
fnd.Replacement.Font.ColorIndex = Word.WdColorIndex.wdBlue;
MysticRyuujin
  • 25
  • 1
  • 8
  • i want to add the custom font color in word using C#.can u please tell me how to do it. – user3217843 Oct 20 '14 at 10:08
  • Not sure if this will work but you can try this: http://stackoverflow.com/questions/15789429/how-to-convert-system-drawing-color-to-microsoft-interop-wdcolorindex Some functions in word are limited to only the available colors. – MysticRyuujin Oct 21 '14 at 10:48
  • Thanks MysticRyuujin for the help.i have already tried that its not working :( – user3217843 Oct 22 '14 at 12:37