0
votes

I have been trying to figure out how to read paragraph content which exists a heading. The heading itself is part of the table of contents. The heading will have a particular style (say Heading 1).

For example: "Introduction" is a entry in Table of content with style Heading 1. I want to read content under heading "Introduction" but not any more content (i.e not content under sub headings of Introduction)

I have been trying to do this using styles/style, TableofContent, Paragraphs/Paragraph,Range. Still cannot come up with a effective solution.

I am working in VB.NET in VS 2010. I am using the word 2007 object model (office 2007 interop) as described @ http://msdn.microsoft.com/en-us/library/bb244515(v=office.12)

Any pointers would be appreciated

regards Sameer

1
So you want to select all content starting from heading 1 and ending at the next heading of any type? Are all the paragraphs to be included of a particular style?Fionnuala
yes that is correct. All the paragraphs are of the same style. Right now i am trying to iterate through all paragraphs, and check paragraph wdbuiltinstyle of wdStyleHeading1. I was thinking once i get a equality i can check for Paragraph text. If that is equal to "Introductions" then i know the next paragraph would be of my interest. I could also crosscheck that this next paragraph has the normal paragraph style. My problem is that paragraph style and builtinstyle do not seem to have same type. I get comException for 'code' p.Style.GetType.Name = WdBuiltinStyle.wdStyleHeading1user179056
You are matching somewhat different things, does p.Style = "Heading 1" not suit?Fionnuala
Hello Remou, Thank you for responding. I tried If p.Style="Heading 1" but then i get System.InvalidCastException. Details have "{"Overload resolution failed because no Public '=' can be called with these arguments:'Public Shared Operator =(a As String, b As String) As Boolean':Argument matching parameter 'a' cannot convert from '__ComObject' to 'String'."} Thank you Sameeruser179056

1 Answers

2
votes

Finally i got the solution.

The code snippet below detects paragraphs which have "Heading 1" style

   For Each paragraph As Paragraph In doc.Paragraphs
        Dim style As NetOffice.WordApi.Style = paragraph.Style
        If style.NameLocal = "Heading 1" Then
            paragraph.Next()
            Console.WriteLine(paragraph.Range.Text)
            Console.WriteLine("*********")
        End If
    Next

Thanks

Sameer