Hi folks,
good monday.
I'm trying to search for some words in my document and then extract them to excel.
It works if I do one search term at a time with 1 block of code.
But I wanted to set up multiple search words - to save me time having to go and change the code again.
and here is where I've got stuck
I'm not sure how to proceed now or if this is the best way
Option Explicit
Sub Word2ExcelCopy()
Dim xlApp As Object
Dim xlBook As Object
Dim xlSheet As Object
Dim oRng As Range
Dim NextRow As Integer
Dim oDoc As Document
'--- Set Up Search conditions here? May be Array
xlSheet.Cells("A" & NextRow, 1) = oRng.Text1
xlSheet.Cells("B" & NextRow, 1) = oRng.Text2
xlSheet.Cells("C" & NextRow, 1) = oRng.Text3
'- Search 1
Set xlApp = GetObject(, "Excel.Application")
Set xlSheet = xlApp.Sheets("Sheet1")
Set oRng = ActiveDocument.Range
With oRng.Find
Do While .Execute(FindText:="Test1")
oRng.Start = oRng.Start + 10
oRng.End = oRng.End - 1 ' Minus the Last Character
NextRow = xlSheet.Range("A" & xlSheet.Rows.Count).End(-4162).Row + 1
xlSheet.Cells(NextRow, 1) = oRng.Text
oRng.Collapse 0
Loop
End With
'-- Search 2
Set xlApp = GetObject(, "Excel.Application")
Set xlSheet = xlApp.Sheets("Sheet1")
Set oRng = ActiveDocument.Range
With oRng.Find
Do While .Execute(FindText:="Test2")
oRng.Start = oRng.Start + 14
oRng.End = oRng.End - 1
NextRow = xlSheet.Range("B" & xlSheet.Rows.Count).End(-4162).Row + 1
xlSheet.Cells(NextRow, 1) = oRng.Text
oRng.Collapse 0
Loop
End With
'-- Search 3
Set xlApp = GetObject(, "Excel.Application")
Set xlSheet = xlApp.Sheets("Sheet1")
Set oRng = ActiveDocument.Range
With oRng.Find
Do While .Execute(FindText:="Test3")
oRng.Start = oRng.Start + 14
oRng.End = oRng.End - 1
NextRow = xlSheet.Range("C" & xlSheet.Rows.Count).End(-4162).Row + 1
xlSheet.Cells(NextRow, 1) = oRng.Text
oRng.Collapse 0
Loop
End With
'--- maybe be more search
End Sub
please do take a look at the code, i tried an array but it was a bit too well mixed between word and excel so i went back to my basic blocks
I'll be really grateful for the help
thank you very much