PDA

View Full Version : Match whole Word



samuelimtech
04-09-2014, 01:53 AM
Morning All,

Ive been given a piece of code from Macropod(what a dude btw) for a find and replace, it didnt quite work as it didnt match the whole word but ive added that but it still doesnt work. below is my code, ive also attached it to give context.


Option Explicit
Sub FindandReplace()

Application.ScreenUpdating = False
Dim RngDoc As Range, oTbl As Table, i As Long
Dim RngFnd As Range, RngRep As Range
With ActiveDocument
Set RngDoc = .Range
Set oTbl = .Tables(.Tables.Count)
RngDoc.End = oTbl.Range.Start
With RngDoc.Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = False
.MatchWholeWord = True
.MatchWildcards = True
For i = 2 To oTbl.Rows.Count
Set RngFnd = oTbl.Cell(i, 1).Range
RngFnd.End = RngFnd.End - 1
Set RngRep = oTbl.Cell(i, 3).Range
RngRep.End = RngRep.End - 1
.Text = RngFnd.Text
.MatchWholeWord = True
.Replacement.Text = RngRep.Text
.Execute Replace:=wdReplaceAll
Next
End With
End With
Set RngFnd = Nothing: Set RngRep = Nothing
Application.ScreenUpdating = True
End Sub


thanks

macropod
04-09-2014, 02:50 AM
MatchWholeWord doesn't work with wildcards, so you need to either: (a) set '.MatchWildcards = False' and not use wildcard Find expressions; or (b) include the wildcard '<' and '>' string delimiters in the expressions to ensure the matched expressions start at a word start and/or end at a word end, respectively.

Whether you need to use wildcards is unclear. In this thread: http://www.vbaexpress.com/forum/showthread.php?49382-Help-with-Regular-Expression, the code clearly needs to use them, but there's no apparent reason to do so for the sample data you provided in this thread: http://www.vbaexpress.com/forum/showthread.php?49383-find-and-replace-using-a-table. In the latter thread, the code I provided specified 'MatchWildcards = True' because that's what your own code had and, at the time, I had no context to work with.

snb
04-09-2014, 02:03 PM
Sub M_snb()
With ThisDocument.Range(ThisDocument.Content.Start, ThisDocument.Tables(1).Range.Start - 1).Find
sn = Split(ThisDocument.Tables(1).Range, vbCr & Chr(7) & vbCr & Chr(7))

For j = 1 To UBound(sn)
sq = Split(sn(j), vbCr & Chr(7))
.Execute sq(0), , True, , , , , , , sq(2), 2
Next

End With
End Sub

macropod
04-09-2014, 03:21 PM
Once again snb, you post code that fails to address the requirements. The OP's data are in the last table, not the first table... in the ActiveDocument, not the template or document the code is run from, which is all ThisDocument achieves. And, without an ounce of explanation, you assume the OP wants to use Match Case and not Match Wildcards. As usual too, no variables are declared - which is liable to cause compile errors, and the Execute arguments aren't named. Surely '.Execute sq(0), , True, , , , , , , sq(2), 2' is less intelligible than '.Execute FindText:=sq(0), MatchWholeWord:=True, Replace:=FRArr(2), Replace:=wdReplaceAll'.

Seems you're more interested in showing how 'clever' you are than in actually posting code that's tailored to their needs and helping them understand how to do things.

samuelimtech
04-10-2014, 12:25 AM
Thank you Macropod apologies for the late reply Ive been Out of the office.
The reason for that is simple the 2 macros do two different things, the first post used wildcards to build the table of references and the second post used that table to renumber the references found.
Its actually driving me mad this as im really quite good with VBA in excel and i cant suss word out at all.
when i mark the .matchwildcard = false as your right that is no longer required and .matchwholeword = true, the code seems to just ignore the matchwholeword function. Ive deduced this is becuase im using a variable to find and im unsure why.

macropod
04-10-2014, 01:13 AM
It has nothing to do with using variables; it's just that '.MatchWholeWord = True' does not work with '.MatchWildcards = True'. Quite unnecessarily, too, your code has two instances of '.MatchWholeWord = True'. To specify a 'wholeword' match with '.MatchWholeWord = True', either add the '<' and '>' to the start and end, respectively, of each find cell, or use code like:

Sub FindandReplace()
Application.ScreenUpdating = False
Dim RngDoc As Range, oTbl As Table, i As Long
Dim RngFnd As Range, RngRep As Range
With ActiveDocument
Set RngDoc = .Range
Set oTbl = .Tables(.Tables.Count)
RngDoc.End = oTbl.Range.Start
With RngDoc.Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = False
.MatchWildcards = True
For i = 2 To oTbl.Rows.Count
Set RngFnd = oTbl.Cell(i, 1).Range
RngFnd.End = RngFnd.End - 1
Set RngRep = oTbl.Cell(i, 3).Range
RngRep.End = RngRep.End - 1
.Text = "<" & RngFnd.Text & ">"
.Replacement.Text = RngRep.Text
.Execute Replace:=wdReplaceAll
Next
End With
End With
Set RngFnd = Nothing: Set RngRep = Nothing
Application.ScreenUpdating = True
End Sub