How to add a paragraph before and after a specific text found with find in a word document via excel
I have managed to put together a code which finds specific text and replaces it with other text. However, I also need it to insert a paragraph before and after the replaced word.
It would look like:
From:
Smth
ToBeReplacedWord
Smth
To:
Smth
Replacement
Smth
My current code is below. Any help would be appreciated.
Option explicit
Sub smth()
Dim sarr as Variant
Dim wat as Object
Dim wa as Object
Dim wd as Object
Dim i as Integer
Dim j as Integer
Dim fn as String
Dim fp as String
sarr = Range("A1").CurrentRegion
fn = dir(Filepath & "\*" & Range("F2").Value &"\*", vbNormal)
fp = Filepath & fn
j= Ubound(sarr, 1)
On Error Resume Next
Set wat = GetObject(, "Word. Application")
Select case true
Case Err.Number <> 0
Set wa = CreateObject("Word. Application")
wa.Visible = true
Set wd = wa.Document.Open(fp)
Case wat = "Microsoft Word"
Set wd = wat.Document.Open(fp)
End select
For i = 1 to j
With wd.Content.Find
.Text = sarr(i, 1)
.ReplacementText = sarr(i, 2)
.MatchWholeWord:=true
.Wrap = 1
.Execute Replace:=1
End with
Next i
Set wat = Nothing
Set wa = Nothing
Set wd = Nothing
End Sub