Find Text Position In word document in C#

MNH ASP 81 Reputation points
2021-12-01T12:48:41.107+00:00

Hello Guys,

I want to find x, y coordinates of particular word in word document using inotrope or any other library in C#
pls suggest me

Thanks!!!

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,245 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 81,721 Reputation points
    2021-12-01T14:59:36.39+00:00

    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);
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful