---
title: "How to extract headings from Word files to Excel?"  
description: "How to extract headings from Word files to Excel?"  
author: "Steilla Mitchel"  
published: 2023-07-19  
updated: 2023-07-19  
canonical: https://www.mindstick.com/forum/159151/how-to-extract-headings-from-word-files-to-excel  
category: "excel"  
tags: ["excel", "ms office"]  
reading_time: 3 minutes  

---

# How to extract headings from Word files to Excel?

How to [extract](https://www.mindstick.com/forum/12883/how-to-extract-sub-string-between-two-string-pattern-using-javascript-jquery) headings from [Word](https://www.mindstick.com/forum/305/read-word-file) [files](https://www.mindstick.com/articles/23302/the-importance-and-advantage-of-keeping-your-important-files-on-the-cloud) to [Excel](https://www.mindstick.com/articles/12937/how-to-import-excel-data-in-sql-server-2014)?

## Replies

### Reply by Aryan Kumar

Sure, here is a VBA code that you can use to extract headings from word files to Excel:

```plaintext
Sub ExtractHeadings()

'Declare variables
Dim doc As Word.Document
Dim headings As Variant
Dim i As Integer

'Get the active document
Set doc = ActiveDocument

'Get all the headings in the document
headings = doc.Range.FindAll("^HEADING", , True, , True, False)

'Create an Excel spreadsheet
Set xlApp = CreateObject("Excel.Application")
Set xlWbk = xlApp.Workbooks.Add
Set xlSht = xlWbk.Sheets.Add

'Loop through all the headings
For i = 1 To UBound(headings)

'Add the heading to the Excel spreadsheet
xlSht.Cells(i, 1).Value = headings(i).Text

Next i

End Sub
```

This code first declares a few variables:

- `doc` is a variable of type `Word.Document` that represents the active document.
- `headings` is a variable of type `Variant` that will contain the list of all the headings in the document.
- `i` is a variable of type `Integer` that will be used to loop through the headings.

The code then gets the active document using the `ActiveDocument` property. It then uses the `FindAll` method to get all the headings in the document. The `FindAll` method takes four arguments:

- The text you want to find.
- A flag that indicates whether or not you want to match the case of the find what text.
- A flag that indicates whether or not you want to match the whole word.
- A flag that indicates whether or not you want to match the find what text in the current selection.

In this case, we are setting the `matchCase` and `matchWholeWord` flags to true. This means that the `FindAll` method will only match headings that are in the same case and that are whole words.

The code then creates an Excel spreadsheet using the `Workbooks.Add` method. It then creates a new worksheet using the `Sheets.Add` method.

The code then loops through all the headings in the document. For each heading, the code adds the heading to the Excel spreadsheet using the `Cells` property.

To use this code, you would first need to insert it into a new or existing Word VBA project. You would then need to run the code. When running the code, the code will extract all the headings from the active document and add them to a new Excel spreadsheet.


---

Original Source: https://www.mindstick.com/forum/159151/how-to-extract-headings-from-word-files-to-excel

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
