4

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
  • 97,193
  • 102
  • 206
  • 364
Charan Gourishetty
  • 109
  • 1
  • 2
  • 10

2 Answers2

3
       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;
         }
  • tmpRange.Text- reading text from doc file sections of the Word doc so you can read text and compare –  Feb 25 '13 at 13:36
  • Have a small doubt ...StoryRanges gets content of each heading ?? – Charan Gourishetty Feb 26 '13 at 06:12
  • Gets a StoryRanges collection that represents all the stories in the document. –  Feb 26 '13 at 06:35
  • I need each heading content Individually... I want to check If one word (My KEY WORD) exits, then read only that particular content .....Stop reading beyond that.... Can u help me with this?? – Charan Gourishetty Feb 26 '13 at 06:52
  • http://social.msdn.microsoft.com/Forums/en-SG/csharplanguage/thread/5e850ab8-cf05-45b4-96a2-7d7500aafa55 - here you can read some example how to read only header. –  Feb 26 '13 at 06:55
  • I Don't need headers...AM asking about the content under one particular heading Like under 1.1.1.1.13 to 1.1.1.1.14 Only....Can I get in this way?? – Charan Gourishetty Feb 26 '13 at 08:23
  • try write some test code and debug, in watch read what is that returning –  Feb 26 '13 at 08:44
  • You forgot word.Quit() in the end. – Bat_Programmer Sep 01 '13 at 02:42
1

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
  • 4,328
  • 2
  • 24
  • 38