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...
Deleted
Sep 15, 2023Thanks for sharing!
Bing AI says this would work:
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
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
r = r + 1
ws.Cells(r, 1).Value = s.Text
ws.Cells(r, 2).Value = s.Information(wdActiveEndPageNumber)
End If
Next s
xl.Visible = True
Bing AI says this would work:
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
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
r = r + 1
ws.Cells(r, 1).Value = s.Text
ws.Cells(r, 2).Value = s.Information(wdActiveEndPageNumber)
End If
Next s
xl.Visible = True