I have a vba code that imports some specific text from a Word document to specific cell in Excel.
Sub ExtractTextFromWordToExcel()
'copy text between two specified strings from Word to Excel.
Dim wordApp As Object
Dim wordDoc As Object
Dim excelApp As Excel.Application
Dim excelWorkbook As Excel.Workbook
Dim excelSheet As Excel.Worksheet
Dim startString As String
Dim endString As String
Dim extractedText As String
Dim i As Long
Dim LRow As Long
ThisWorkbook.Sheets("List2").Select
'find last row
ActiveSheet.UsedRange 'Refresh UsedRange
LRow = ActiveSheet.UsedRange.Rows(ActiveSheet.UsedRange.Rows.Count).Row
' Create a Word application object
Set wordApp = CreateObject("Word.Application")
' Open the Word document
Set wordDoc = wordApp.Documents.Open("C:\MyPath\MyFile.docx") ' Replace with your Word file path
For i = 2 To LRow
If ActiveSheet.Range("B" & i) = "" Then
' Set the start and end strings for text extraction
startString = ActiveSheet.Range("C" & i)
endString = ActiveSheet.Range("D" & i)
Else
' Set the start and end strings for text extraction
startString = ActiveSheet.Range("B" & i) & vbTab '& ActiveSheet.Range("C" & i)
endString = ActiveSheet.Range("D" & i) & vbTab & ActiveSheet.Range("E" & i)
End If
' Extract the text between the start and end strings
extractedText = wordDoc.Content
extractedText = Split(extractedText, startString)(1)
extractedText = Split(extractedText, endString)(0)
' ' Create an Excel application object
' Set excelApp = Excel.Application
'
' ' Add a new workbook
' Set excelWorkbook = excelApp.Workbooks.Add
'
' ' Set a reference to the first sheet in the workbook
' Set excelSheet = excelWorkbook.Sheets(1)
' Paste the extracted text into cell A1 of the Excel sheet
' excelSheet.Range("D2").Value = extractedText
ActiveSheet.Range("A" & i).Value = extractedText
Next i
' Close the Word document and quit the Word application
wordDoc.Close SaveChanges:=False
wordApp.Quit
' Save the Excel workbook
' excelWorkbook.SaveAs "C:\path\to\save\excel\file.xlsx" ' Replace with your desired Excel file path
' Close the Excel workbook and quit the Excel application
' excelWorkbook.Close SaveChanges:=False
' excelApp.Quit
' Release the objects from memory
Set wordDoc = Nothing
Set wordApp = Nothing
' Set excelSheet = Nothing
' Set excelWorkbook = Nothing
' Set excelApp = Nothing
MsgBox "Text extracted and saved to Excel successfully!"
End Sub
This code works, but I'd like to be able to search for Word document to be opened.
I have replaced this part of code
' Create a Word application object
Set wordApp = CreateObject("Word.Application")
' Open the Word document
Set wordDoc = wordApp.Documents.Open("C:\MyPath\MyFile.docx") ' Replace with your Word file path
with following code
' Create an instance of Word application
Set wordApp = CreateObject("Word.Application")
' Display the file picker dialog
filePath = Application.GetOpenFilename("Word Documents (*.docx), *.docx")
' Check if a file was selected
If filePath <> False Then
' Open the selected Word document
Set wordDoc = wordApp.Documents.Open(filePath)
' Make Word visible (optional)
wordApp.Visible = True
End If
Unfortunately, this code does not work as I expected. It opens the selected Word document, but does not copy the text and reports the error "Run-time error '9', Subscription out of range" for this line in 'Extract the text between the start and end strings
part:
extractedText = Split(extractedText, startString)(1)
Any advice on how to fix the error would be appreciated.