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);
}

Sukesh GandhamRank #8,774 · 216 reputationPosted Aug 8, 2018, 2:53 AM EDT
Marvin ReidRank #1,963 · 746 reputationPosted Jun 22, 2023, 2:21 PM EDT
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:
// Initialize the OCR Engine using (IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD)) { // Load the document to be analyzed using (LEADDocument document = DocumentFactory.LoadFromFile(@"C:\Desktop\test.docx", new LoadDocumentOptions())) { document.Text.OcrEngine = ocrEngine; // Initialize the Document Analyzer var analyzer = new DocumentAnalyzer() { Reader = new UnstructuredDataReader(), QueryContext = new FileRepositoryContext(@"C:\Desktop\test.json") }; // Initialize the Document Options var options = new DocumentAnalyzerRunOptions() { ElementQuery = new RepositoryQuery() }; // Create the list of results List results = analyzer.Run(document, options); string resultsMessage = string.Empty; // Parse the results and output text to console foreach (ElementSetResult result in results) foreach (ElementResult item in result.Items) Console.Write($"{(item.Value)} "); }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
Deepak RawatRank #646 · 3.7K reputationPosted Jun 23, 2023, 12:33 AM EDT
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
using Word = Microsoft.Office.Interop.Word; using System.Linq; string fileName = "D:\\Files\\Scan.doc"; // The file path goes here string keyword1 = "Keyword1"; // The first keyword string keyword2 = "Keyword2"; // The second keyword Word.Application wordApp = new Word.Application(); Word.Document doc = null; try { doc = wordApp.Documents.Open(fileName); doc.Activate(); List linesBetweenKeywords = new List(); bool isBetweenKeywords = false; foreach (Word.Range wordRange in doc.Words) { string wordText = wordRange.Text.Trim(); if (wordText.Equals(keyword1, StringComparison.CurrentCultureIgnoreCase)) { isBetweenKeywords = true; } else if (wordText.Equals(keyword2, StringComparison.CurrentCultureIgnoreCase)) { isBetweenKeywords = false; break; // Stop searching once the second keyword is found } if (isBetweenKeywords) { linesBetweenKeywords.Add(wordText); } } // Print the lines between the keywords foreach (string line in linesBetweenKeywords) { Console.WriteLine(line); } } catch (Exception ex) { Console.WriteLine("Error: " + ex.Message); } finally { // Close and release the Word objects if (doc != null) { doc.Close(); Marshal.ReleaseComObject(doc); } if (wordApp != null) { wordApp.Quit(); Marshal.ReleaseComObject(wordApp); } }Make sure to add the necessary using directives at the beginning of your code file:
using Word = Microsoft.Office.Interop.Word; using System.Linq; using System.Runtime.InteropServices;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.