Lokesh Sharma

Lokesh Sharma

  • NA
  • 4
  • 749

Extract lines between two keywords in Word Document

Aug 7 2018 6:40 AM
Need to search for two keywords in Word document
 
E.g..
 
Keyword1
Keyword2
 
Lines between both the keywords then needs to be returned.
 
Trying this but i couldn't get this to work even for single keyword search
 
 using word = Microsoft.Office.Interop.Word;

object fileName = "D:\\Files\\Scan.doc"; //The filepath goes here
string textToFind = "Keyword1"; //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);
doc.Activate();
foreach (Word.Range docRange in doc.Words)
{
if (docRange.Text.Trim().Equals(textToFind,
StringComparison.CurrentCultureIgnoreCase))
{
Response.Write(docRange.Text);
}
}
}
catch (Exception ex)
{
Response.Write("Error : " + ex.Message);
}


Answers (3)