How to extract headings from Word files to Excel?
Ask by Steilla Mitchel
Updated 19 Jul 2023
Sure, here is a VBA code that you can use to extract headings from word files to Excel:
This code first declares a few variables:
docis a variable of typeWord.Documentthat represents the active document.headingsis a variable of typeVariantthat will contain the list of all the headings in the document.iis a variable of typeIntegerthat will be used to loop through the headings.The code then gets the active document using the
ActiveDocumentproperty. It then uses theFindAllmethod to get all the headings in the document. TheFindAllmethod takes four arguments:In this case, we are setting the
matchCaseandmatchWholeWordflags to true. This means that theFindAllmethod 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.Addmethod. It then creates a new worksheet using theSheets.Addmethod.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
Cellsproperty.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.