PDA

View Full Version : Find and Replace within Word Tables



ajhez
07-24-2017, 06:01 AM
Hi guys,

Any ideas why the below find and replace code isn't finding '£' located in tables within a word document?

The code finds and replaces all of the £ into € that are in the body of the document, but for some reason it doesn't find all of the '£' that are in word tables with the document.



Private Sub Test()
If ComboBox14.Text <> "€" Then
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "£"
.Replacement.Text = "€"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.CorrectHangulEndings = False
.HanjaPhoneticHangul = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
.MatchFuzzy = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub


Any ideas?

Thanks,
AJHEZ

mana
07-24-2017, 06:33 AM
Option Explicit

Sub test()
Dim t As Table

For Each t In ActiveDocument.Tables
t.Range.Find.Execute FindText:="aa", ReplaceWith:="bb", Replace:=wdReplaceAll
Next

End Sub

ajhez
07-24-2017, 07:02 AM
Thanks, mana.

Would this also find the values outside the tables?

Both values inside and outside the tables need to be found and replaced - is it possible to do this in one macro?

mana
07-24-2017, 07:15 AM
???
>Title : Find and Replace within Word Tables


Sub test2()
ActiveDocument.Range.Find.Execute FindText:="aa", ReplaceWith:="bb", Replace:=wdReplaceAll
End Sub

ajhez
07-24-2017, 07:15 AM
I think the issue may actually be that it is failing to change £ to € where the text has a number straight after it.

e.g. it will change £ 1,000 (where there is a space) but it will not change £1,000...

Kilroy
07-25-2017, 05:50 AM
Minor changes



Sub Test()
Selection.WholeStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "£"
.Replacement.Text = "€"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.CorrectHangulEndings = False
.HanjaPhoneticHangul = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
.MatchFuzzy = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

The code Mana posted works perfect as well when you put your characters in.


Sub test2()
ActiveDocument.Range.Find.Execute FindText:="£", ReplaceWith:="€", Replace:=wdReplaceAll
End Sub

ajhez
07-26-2017, 01:43 AM
Thanks, both - this has been a massive help!

AJHEZ