Mar 06 2024 12:34 PM - edited Mar 06 2024 12:35 PM
Hi everyone, if anyone has experience with macros/done the below already, I would appreciate any insight:
There are about 100 word documents that are already in a folder where I need to extract certain information after specific words and place it in an Excel spreadsheet. For example, in the following fields of the Word document:
Name: Ethan
Date: March 6 2024
"Ethan" and "March 6, 2024" would be extracted into two separate columns (same row) in another Excel sheet. If anyone has done this/could link to a resource that would be great!
Mar 06 2024 09:40 PM
To accomplish this task, you can use VBA macros in both Word and Excel to automate the extraction of information from multiple Word documents and populate an Excel spreadsheet with the extracted data. Below are the steps to achieve this:
Word VBA Macro:
Vba code is untested, please backup your file.
Sub ExtractDataFromWord()
Dim wrdApp As Object ' Word.Application
Dim wrdDoc As Object ' Word.Document
Dim strFile As String
Dim iRow As Long
' Initialize Excel
Dim xlApp As Object ' Excel.Application
Dim xlWb As Object ' Excel.Workbook
Dim xlWs As Object ' Excel.Worksheet
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlWb = xlApp.Workbooks.Add
Set xlWs = xlWb.Sheets(1)
' Specify the folder path containing Word documents
strFolder = "C:\YourFolderPath\" ' Update this with the path to your folder
strFile = Dir(strFolder & "*.docx") ' Modify the file extension if necessary
' Loop through each Word document in the folder
Do While strFile <> ""
' Open the Word document
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = False
Set wrdDoc = wrdApp.Documents.Open(strFolder & strFile)
' Extract data and write to Excel
iRow = xlWs.Cells(xlWs.Rows.Count, 1).End(-4162).Row + 1 ' Find the next available row in Excel
xlWs.Cells(iRow, 1).Value = wrdDoc.Range.Paragraphs(1).Next.Paragraphs(1).Range.Text ' Extract Name
xlWs.Cells(iRow, 2).Value = wrdDoc.Range.Paragraphs(2).Next.Paragraphs(1).Range.Text ' Extract Date
' Close the Word document
wrdDoc.Close False
wrdApp.Quit
' Move to the next Word document
strFile = Dir
Loop
' Cleanup
Set wrdApp = Nothing
Set wrdDoc = Nothing
Set xlApp = Nothing
Set xlWb = Nothing
Set xlWs = Nothing
MsgBox "Data extraction completed!", vbInformation
End Sub
5. Modify the following parts of the code:
6. Close the VBA editor.
Excel VBA Macro:
Vba code:
Sub ExtractDataFromWordDocuments()
Dim objWord As Object ' Word.Application
Dim objDoc As Object ' Word.Document
Dim strFolder As String
Dim strFile As String
Dim iRow As Long
' Initialize Excel
Dim xlApp As Object ' Excel.Application
Dim xlWb As Object ' Excel.Workbook
Dim xlWs As Object ' Excel.Worksheet
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlWb = xlApp.Workbooks.Add
Set xlWs = xlWb.Sheets(1)
' Specify the folder path containing Word documents
strFolder = "C:\YourFolderPath\" ' Update this with the path to your folder
strFile = Dir(strFolder & "*.docx") ' Modify the file extension if necessary
' Loop through each Word document in the folder
Do While strFile <> ""
' Open the Word document
Set objWord = CreateObject("Word.Application")
objWord.Visible = False
Set objDoc = objWord.Documents.Open(strFolder & strFile)
' Extract data and write to Excel
iRow = xlWs.Cells(xlWs.Rows.Count, 1).End(-4162).Row + 1 ' Find the next available row in Excel
xlWs.Cells(iRow, 1).Value = objDoc.Range.Paragraphs(1).Next.Paragraphs(1).Range.Text ' Extract Name
xlWs.Cells(iRow, 2).Value = objDoc.Range.Paragraphs(2).Next.Paragraphs(1).Range.Text ' Extract Date
' Close the Word document
objDoc.Close False
objWord.Quit
' Move to the next Word document
strFile = Dir
Loop
' Cleanup
Set objWord = Nothing
Set objDoc = Nothing
Set xlApp = Nothing
Set xlWb = Nothing
Set xlWs = Nothing
MsgBox "Data extraction completed!", vbInformation
End Sub
5. Modify the following parts of the code:
6. Close the VBA editor.
Running the Macro:
This macro will loop through all the Word documents in the specified folder, extract the Name and Date information, and populate an Excel spreadsheet with the extracted data. Make sure to adjust the file path and extensions as necessary. The text, steps and the code were created with the help of AI.
Hope this will help you.
Was the answer useful? Mark as best response and like it!
This will help all forum participants.