Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I want to get all the headings along their sub-headings separately from a word file programmatically Using c# for example i have following content :

HEADING 1 XYZ
heading 2
heading 3


HEADING 1 ABC

HEADING 1 DEF
heading 2 lorem ispum

so my code should return me:

Heading 1 XYZ
heading 2
heading 3


seperately and similarly remaining headings and subheadings also.
I have tried the following code

C#
foreach (Microsoft.Office.Interop.Word.Paragraph paragraph in oMyDoc.Paragraphs )
                {
                    Microsoft.Office.Interop.Word.Style style = paragraph.get_Style() as Microsoft.Office.Interop.Word.Style;
                    string styleName = style.NameLocal;
                    string text = paragraph.Range.Text;


                    if (styleName == "Title")
                    {
                        title = text.ToString();
                    }
                    else if (styleName == "Subtitle")
                    {
                        st = text.ToString() + "\n";

                    }
                    else if (styleName=="Heading 1")
                    {
                        heading1[h1c] = text.ToString()+"\n";


                    }

}
Posted
Updated 12-Jan-15 4:40am
v2
Comments
CHill60 12-Jan-15 8:24am    
What have you tried?
Member 11255065 12-Jan-15 8:50am    
--> code snippet to question
It returns me all the headings and subheadings together not seperately
CHill60 12-Jan-15 9:04am    
I'm still not sure what you mean by "separately" - This would produce the sample of expected output you put into your question.
Did you want
Heading 1 XYZ
Heading 1 ABC
Heading 1 DEF

then

heading 2
heading 3
heading 2 lorem ipsum ??
Member 11255065 12-Jan-15 9:33am    
Nope actually i want to store each of the heading and its subheading separately so that i may divide the entire content on the basis of headings.
sathish.kasin 19-Nov-19 0:35am    
HiAny luck on this?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900