Copy paragraph with certain keyword available under a searched word

Jmac2604

New Member
Joined
Jun 11, 2021
Messages
15
Office Version
  1. 365
Platform
  1. Windows
Hi,
I am new to word automation. I got the below code which will search for a keyword in word and will extract the second paragraph below it. But I want is instead takin the second paragraph blindly it needs extract the paragraph based on a specific word in the beginning. Please help me on this if possible.

My code is


Option Compare Text

Sub LocateSearchItem()
Application.EnableEvents = False
Dim shtSearchItem As Worksheet
Dim shtExtract As Worksheet
Dim oWord As Word.Application
Dim WordNotOpen As Boolean
Dim oDoc As Word.Document
Dim oRange As Word.Range
Dim LastRow As Long
Dim CurrRowShtSearchItem As Long
Dim CurrRowShtExtract As Long
Dim myPara As Long
Dim rCell As Range
Dim str
Dim objPic As InlineShape
Dim i As Long
Dim LR As Long
Dim MyString As String, newString As String
Dim strFolder As String, strFile As String
Dim rng
Dim iPage As Integer

Dim myRange As Range
Dim MyCell As Range
Dim splitText As Variant
Dim Ws As Worksheet

Set ws2 = ThisWorkbook.Worksheets("Defect des_backup")
Set ws3 = ThisWorkbook.Worksheets("Defects")



On Error Resume Next


WordApp.Visible = True

Set oWord = GetObject(, "Word.Application")

oWord.Visible = True

oWord.Activate

strFolder = ActiveWorkbook.Path

strFile = Dir(strFolder & "\" & ThisWorkbook.Worksheets("Defects").Range("H1").value, vbNormal)

Set oDoc = oWord.Documents.Open(Filename:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=True, ReadOnly:=True) ' <= modify according to your path
oWord.Visible = True

With oDoc

Set rng = .GoTo(What:=wdGoToPage, Name:=2)

Set rng = rng.GoTo(What:=wdGoToBookmark, Name:="\page")

rng.delete

End With

Set shtSearchItem = ThisWorkbook.Worksheets("Defects")

If ThisWorkbook.Worksheets.Count < 2 Then

ThisWorkbook.Worksheets.Add After:=shtSearchItem

End If

Set shtExtract = ThisWorkbook.Worksheets("Defect des_backup")

LastRow = ws3.Range("D" & Rows.Count).End(xlUp).Row

CurrRowShtExtract = 2

For CurrRowShtSearchItem = 2 To LastRow


Set oRange = oDoc.Range

With oRange.Find

.Text = shtSearchItem.Cells(CurrRowShtSearchItem, 4).Text

.MatchCase = False

.MatchWholeWord = True

While oRange.Find.Execute = True

oRange.Select

myPara = oDoc.Range(0, oWord.Selection.Paragraphs(1).Next(Count:=2).Range.End).Paragraphs.Count

shtExtract.Cells(CurrRowShtExtract, 1).value = .Text

'shtExtract.Cells(CurrRowShtExtract, 3).Value = myPara

shtExtract.Cells(CurrRowShtExtract, 2) = oDoc.Paragraphs(myPara).Range

oRange.Collapse wdCollapseEnd

Wend

End With

CurrRowShtExtract = CurrRowShtExtract + 1

Next

Set oWord = Nothing

Set oDoc = Nothing


Application.EnableEvents = True
End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

Forum statistics

Threads
1,223,231
Messages
6,170,885
Members
452,364
Latest member
springate

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top