Friday, August 24, 2018

How to get word document headings to into excel

I'm trying to import the headings of word document to an excel spreadsheet.

Word Headings:

1 Heading 1

1.1 Heading 2

1.1.1 Heading 3

The spreadsheet would have columns Heading 1, Heading 2, Heading 3

Table example

Solved

I'm sorry there was a big mistake in my first answer. You don't need the application object to create your word app.

The correct code see below:

Option Explicit

Public Sub Test_ReadWordDocument()
    Dim WordApp     As Object
    Dim WordDoc     As Object
    Dim Para        As Object

    Set WordApp = CreateObject("Word.Application")
    Set WordDoc = WordApp.Documents.Open("...\TestReadWordDoc.docx")

    For Each Para In WordDoc.Paragraphs
        Debug.Print Para.Range.Text
    Next Para

End Sub

Result:

Heading1
Heading2
Heading3
Lore ipsum Lore ipsum 
Lore ipsum 
Lore ipsum 
Lore ipsum

From there you can go on. Use the locals window to inspect the late binded objects. :)


No comments:

Post a Comment