suppose i have to search multiple string in MS-Word document. i want to pass multiple keyword to word api and i need that api will open that doc or docx file by MS-word and highlight those word if found in ms-word file supplied by me. here i got a sample code for highlight words in ms-word file but the routine i found may not highlight multiple word. another problem i notice that when it highlight the file and open then it works fine but when i close the ms-word then it asking for saving changes. i understand that this routine modify the document for making the highligh which i do not want. i want that routine will highlight but will not modify the doc file....is there any way to do it. please guide. thanks
using Word = Microsoft.Office.Interop.Word;
private void btnFind_Click(object sender, EventArgs e)
{
object fileName = "audi.doc"; //The filepath goes here
string textToFind = "test1,test2,test3"; //The text to find goes here
Word.Application word = new Word.Application();
Word.Document doc = new Word.Document();
object missing = System.Type.Missing;
try
{
doc = word.Documents.Open(ref fileName, 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);
doc.Activate();
foreach (Word.Range docRange in doc.Words)
{
if(docRange.Text.Trim().Equals(textToFind,
StringComparison.CurrentCultureIgnoreCase))
{
docRange.HighlightColorIndex =
Microsoft.Office.Interop.Word.WdColorIndex.wdDarkYellow;
docRange.Font.ColorIndex =
Microsoft.Office.Interop.Word.WdColorIndex.wdWhite;
}
}
System.Diagnostics.Process.Start(fileName.ToString());
}
catch (Exception ex)
{
MessageBox.Show("Error : " + ex.Message);
}
}