Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find and Replace Problem. Is this possible? 1

Status
Not open for further replies.

darkojones

Programmer
Sep 14, 2006
2
GB
Hi everyone. I'm hoping you can help me.

I've got a word document template with IF statements and MergeFields. Our templates have IF statements in the format

{ IF { MERGEFIELD Contact_Ref } = 6825508 "True" "False" }

However, for some reason its been decided that all our templates must now have IF statements in the format.

{ IF "{ MERGEFIELD Contact_Ref }" = 6825508 "True" "False" }

With quotation marks around the first argument of the IF statements.

I've been playing around with a VBA Macro using the Word Find and Replace functions and regular expressions to match the pattern but I've run in to problems because there is no way to match a Mergefield.

What I'd like to know is if anyone else has had this problem? Is it actually possible ?

Any thoughts on how to do it would be greatfully received.

Andy
 
Here's the macro I've been using if that is anyhelp. This is my first experience of VBA so I've kind of been thrown in at the deep end.



Public Sub IFReplaceArrays()
Const arraySize As Integer = 25

Dim oldRef(0 To arraySize) As String
oldRef(0) = "IF *"
oldRef(1) = "[ ][/>]"
oldRef(2) = "[ ][/=]"
oldRef(3) = "[ ][/<]"
oldRef(4) = "[ ][/</>]"
oldRef(5) = "[ ][/</=]"
oldRef(6) = "[ ][/>/=]"
oldRef(7) = " [/>]"
oldRef(8) = " [/=]"
oldRef(9) = " [/<]"
oldRef(10) = " [/</>]"
oldRef(11) = " [/</=]"
oldRef(12) = " [/>/=]"
oldRef(13) = " [/>]"
oldRef(14) = " [/=]"
oldRef(15) = " [/<]"
oldRef(16) = " [/</>]"
oldRef(17) = " [/</=]"
oldRef(18) = " [/>/=]"
oldRef(19) = " [/>]"
oldRef(20) = " [/=]"
oldRef(21) = " [/<]"
oldRef(22) = " [/</>]"
oldRef(23) = " [/</=]"
oldRef(24) = " [/>/=]"

Dim newRef(0 To arraySize) As String

newRef(0) = "IF """
newRef(1) = """>"
newRef(2) = """="
newRef(3) = """<"
newRef(4) = """<>"
newRef(5) = """<="
newRef(6) = """>="
newRef(7) = """>"
newRef(8) = """="
newRef(9) = """<"
newRef(10) = """<>"
newRef(11) = """<="
newRef(12) = """>="
newRef(13) = """>"
newRef(14) = """="
newRef(15) = """<"
newRef(16) = """<>"
newRef(17) = """<="
newRef(18) = """>="
newRef(19) = """>"
newRef(20) = """="
newRef(21) = """<"
newRef(22) = """<>"
newRef(23) = """<="
newRef(24) = """>="

Call replaceFields(oldRef, newRef, arraySize)

End Sub

Sub replaceFields(oldRef() As String, newRef() As String, arraySize As Integer)
'
Dim x As Integer
Dim MyFile As String
Dim rngStory As Range

MyFile = Dir$("\\sanfs\home\Lawsona\documents\ReplaceFields\*.dot")

Application.ScreenUpdating = False
Application.DisplayAlerts = wdAlertsNone

Do While MyFile <> ""

Documents.Open FileName:="\\sanfs\home\Lawsona\documents\ReplaceFields\" & MyFile

With ActiveDocument

ActiveWindow.View.ShowFieldCodes = True



For Each rngStory In ActiveDocument.StoryRanges

With rngStory.Find

For x = 0 To arraySize

.Text = oldRef(x)

.Replacement.Text = newRef(x)

.Wrap = wdFindContinue

.MatchCase = True

'.MatchWholeWord = True

.MatchWholeWord = False

.MatchWildcards = True

.Execute Replace:=wdReplaceAll

Next x

End With

Next rngStory
ActiveWindow.View.ShowFieldCodes = False



End With

Selection.WholeStory
Selection.Copy
Documents.Add DocumentType:=wdNewBlankDocument
Selection.PasteAndFormat (wdPasteDefault)
ActiveDocument.SaveAs FileName:= _
"C:\Documents and Settings\lawsona\Desktop\" & MyFile, FileFormat:= _
wdFormatTemplate, LockComments:=False, Password:="", AddToRecentFiles:= _
True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:= _
False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False
ActiveDocument.Close
MyFile = Dir
Loop

Application.ScreenUpdating = True
Application.DisplayAlerts = wdAlertsAll

End Sub
 
darkojones,
I think you may be taking the hard route. The [tt]MailMergeField[/tt]'s are in a collection you can iterate through. Once you have each [tt]MailMergeField[/tt] you can manipulate the [tt]Code.Text[/tt].

Here is a quick and dirty routine to illistrate (Word 2k SR-1):
Code:
Public Sub List_MailMergeDataFields()
Dim mmf As MailMergeField
Dim strCodeText As String
For Each mmf In ActiveDocument.MailMerge.Fields
  mmf.Code.Text = Replace(mmf.Code.Text, "{", Chr(34) & "{")
  mmf.Code.Text = Replace(mmf.Code.Text, "}", "}" & Chr(34))
Next mmf
End Sub

Hope this helps,
CMP

[small]For the best results do what I'm thinking, not what I'm saying.[/small]
(GMT-07:00) Mountain Time (US & Canada)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top