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);
}
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);
}
Deepak RawatPosted Jun 23, 2023, 4:33 AM
Hi, It seems like you're using the Microsoft Office Interop library to search for keywords in a Word document. However, there are a few issues with your code
Make sure to add the necessary using directives at the beginning of your code file:
This code will search for the lines between
Keyword1andKeyword2in the Word document specified by thefileNamevariable. The lines will be stored in thelinesBetweenKeywordslist and then printed out. Remember to modify thefileName,keyword1, andkeyword2variables according to your specific requirements.Marvin ReidPosted Jun 22, 2023, 6:21 PM
A solution you can look into is leveraging the DocumentAnazlyer class which allows you to specify a ruleset contained within a Json file. The ruleset would allow you to map exactly what kind of words you want to look out for. With this you'll be able to parse the results and know exactly where the words are located within the file. Here is a snippet which demonstrates this:
https://www.leadtools.com/help/sdk/dh/doxa/documentanalyzer-run.html
https://www.leadtools.com/help/sdk/tutorials/dotnet-console-parse-data-with-the-document-analyzer.html
Sukesh GandhamPosted Aug 8, 2018, 6:53 AM