PDA

View Full Version : Find and replace whole word only [code attached]



smallxyz
11-21-2015, 12:43 AM
Dear all,

I have a set of words to be replaced in ppt. The requirement is, ONLY whole words found to be replaced.

e.g.
Given words "done","money"and "one", replaced "one" by "1"

My desired result : "done", "money", "1"

but not : "d1", "m1y", "1"

Below is a code I found on net. But it would also replace word fragments. How to amend the code so that only WHOLE words are replaced?

Thank you!


Sub f_rp(f As String, rp As String)

Dim sld As Slide
Dim grpItem As Shape
Dim shp As Shape
Dim i As Long
Dim j As Long

For Each sld In ActivePresentation.Slides
For Each shp In sld.Shapes


If shp.HasTextFrame Then
If shp.TextFrame.HasText Then
shp.TextFrame.TextRange.Text = Replace(shp.TextFrame.TextRange.Text, f, rp, , , vbTextCompare)
End If
End If


If shp.HasTable Then
For i = 1 To shp.Table.Rows.Count
For j = 1 To shp.Table.Columns.Count
shp.Table.Rows.Item(i).Cells(j).Shape.TextFrame.TextRange.Text = _
Replace(shp.Table.Rows.Item(i).Cells(j).Shape.TextFrame.TextRange.Text, f, rp, , , vbTextCompare)
Next j
Next i
End If


Next shp
Next


End Sub

John Wilson
11-21-2015, 12:56 PM
You cannot specify whole words using the string replace method.

The way to get what you need is to use the TextRange.Replace method

There's a worked example on Shyam's pages

http://skp.mvps.org/ppt00025.htm#2

smallxyz
11-24-2015, 05:00 AM
Thank you !