11,581 questions
A way with Word Interop :
Microsoft.Office.Interop.Word.Application oWordApplication = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Documents oWordDocuments = oWordApplication.Documents;
Microsoft.Office.Interop.Word.Document oWordDocument = oWordDocuments.Open(@"e:\\test.doc");
//oWordApplication.Visible = true;
object oFindText = "test";
object oMissing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Range range = oWordApplication.ActiveDocument.Content;
while (true)
{
range.Find.Execute(
ref oFindText, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
if (range.Find.Found)
{
int nLine = range.get_Information(Microsoft.Office.Interop.Word.WdInformation.wdFirstCharacterLineNumber);
int nColumn = range.get_Information(Microsoft.Office.Interop.Word.WdInformation.wdFirstCharacterColumnNumber);
MessageBox.Show(string.Format("Line = {0} - Column {1}\r\n", nLine, nColumn), "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
break;
}
oWordApplication.Quit();
Marshal.ReleaseComObject(oWordDocument);
Marshal.ReleaseComObject(oWordDocuments);
Marshal.ReleaseComObject(oWordApplication);