Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I extract text formatted with certain styles from word document?

Tags:

ms-word

vba

I have a very long and complex word document (200+ pages), and would like to extract all content formatted with a certain style, while skipping the rest of the document. It is easy if to do as long as you only need to find one style - but i'm looking for a solution that can extract various styles (ie all headings AND all text formatted as style2).

like image 501
Hampus Brynolf Avatar asked Dec 06 '25 20:12

Hampus Brynolf


1 Answers

If you can manage to copy all the test to another document then run this then great, but here's a good start - this is how you can loop through and delete everything that is not of the style you want to keep.

You could do a few if statements instead to check each style, but using a string of all the ok styles and using instr is nice.

Sub DeleteUnwatedFormats()

Dim para As Paragraph

Dim okStyles As String
okStyles = "Normal, Heading1, Heading2" 'list up ok styles

For Each para In ActiveDocument.Paragraphs
    If InStr(1, okStyles, para.Style) = 0 Then
        para.Range.Delete
    End If
Next

End Sub
like image 181
aevanko Avatar answered Dec 09 '25 17:12

aevanko