Forum Discussion
Samantha4183
Sep 15, 2023Copper Contributor
Extract sentences with a specific word & page number from Word into Excel
I have a very large Word document where a specific word appears hundreds of times. I would like to extract all of the sentences where the word appears into Excel, including the page number the senten...
Sep 17, 2023
Samantha4183 Use the following in which the changes to your original code are shown in red.
Const t = "sunshine" ' the text to search for
Dim s As Range
Dim xl As Object
Dim wb As Object
Dim ws As Object
Dim r As Long
Dim p As Long
Set xl = CreateObject("Excel.Application")
Set wb = xl.Workbooks.Add(Template:=-4167) ' xlwbatworksheet
Set ws = wb.Worksheets(1)
For Each s In ActiveDocument.Sentences
If InStr(1, s.Text, t, vbTextCompare) > 0 Then
p = s.Information(wdActiveEndPageNumber)
r = r + 1
ws.Cells(r, 1).Value = s.Text & " from page " & p
End If
Next s
xl.Visible = True