Looping through Word document for styles
Hi,
I want to loop through all the lines of a word document and get the style of that sentence, like Heading 1, Heading 2, normal etc.
How can I do this?
I know how to open a word document, but I don't know how to loop through all the sentences and grab the style.
Thanks.
vbcode1980
Re: Looping through Word document for styles
VB Code:
Dim p As Paragraph
For Each p In ActiveDocument.Paragraphs
Debug.Print p.Style, p.Range.Font.Name, p.Range.Font.Size
Next
Re: Looping through Word document for styles
Is there a way to find out the name of the heading like "Heading 1" or "Heading 3"?
Re: Looping through Word document for styles
Quote:
Is there a way to find out the name of the heading like "Heading 1" or "Heading 3"?
That is exactly what p.Style gives you. I just threw in the font name and size in case you were interested.
I ran the code I gave you against a document with Heading1 and Heading2 styles and it listed them as expected. What results are you getting?
Re: Looping through Word document for styles
Hmm, I didn't get the heading names. Maybe there's something wrong with my test documents.
I'll try again.
Thanks :)
Re: Looping through Word document for styles
The code I gave you was a macro running in Word. It should be exactly the same in VB6 working on the ActiveDocument object.
What result do you get if you run the macro in word? If it works ok then I'd guess that your Word object isn't pointing at the document you think it is.
Re: Looping through Word document for styles
Strange..
Anyway, my boss wants it in .NET now, so I'm moving to the .NET forum.s
Because if I use this code in .NET I get 'System._ComObject' for style... :confused: