2

I need to start reading a word document from a specific point. That key word is taken from a dropdown combo box. The keyword is something like [blah blah, blah, 001]

So, I need to read only the content from that keyword to next heading ...

I used this to read heading numbers and line by line but heading num notworking

string headNum = objparagraph.Range.ListFormat.ListString;
string sLine = objparagraph.Range.Text;
Soner Gönül
  • 94,086
  • 102
  • 195
  • 339

2 Answers2

2
       Word.Application word = new Word.Application();
       Word.Document doc = new Word.Document();
       object fileName = @"C:\wordFile.docx";
        // Define an object to pass to the API for missing parameters
        object missing = System.Type.Missing;                
        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, ref missing);
        string ReadValue = string.Empty;
            // Activate the document
        doc.Activate();

         foreach (Word.Range tmpRange in doc.StoryRanges)
         {
            ReadValue += tmpRange.Text;
         }
0

If I understood correctly, you need to read the Word document starting from your keyword to next heading. In other words, something like the red text in the following document:

enter image description here

In that case, here is how you can accomplish that with GemBox.Document:

string keyword = " [blah blah, blah, 001]";
DocumentModel document = DocumentModel.Load("input.docx");

ContentPosition start = document.Content
    .Find(keyword)
    .First()
    .End;

ContentPosition end = new ContentRange(start, document.Content.End)
    .GetChildElements(ElementType.Paragraph)
    .Cast<Paragraph>()
    .First(p => p.ParagraphFormat.Style != null && p.ParagraphFormat.Style.Name.Contains("heading"))
    .Content
    .Start;

string text = new ContentRange(start, end).ToString();

The text variable's value will be:

Sample text content that we want to retrieve.
Another sample paragrap.

Also, here are additional Reading and Get Content examples, they contain some useful information.

Mario Z
  • 3,828
  • 2
  • 25
  • 37