MS Office Forum / Word / Mailmerge and Fax / October 2006
Changing Merge Field Names
|
|
Thread rating:  |
Microsoft Newsserver - 06 Oct 2006 06:58 GMT I need to change the merge fields in a number of documents. For example one of the old merge fields is:
«Contactsdear»
The new merge field is:
«dear»
(i.e. I've just deleted "Contacts" from the merge field name). I've tried using find/replace to do this but it doesn't appear to work on merge fields as they keep reverting back to the original format.
Does anyone know what I'm doing wrong or if this is indeed possible?
Many thanks, Andrew
Graham Mayor - 06 Oct 2006 07:30 GMT Replace will work on field codes provided they are toggled to display the construction (ALT+F9)
{ Mergefield ContactsDear }
You may find http://www.gmayor.com/batch_replace.htm helpful.
 Signature <>>< ><<> ><<> <>>< ><<> <>>< <>><<> Graham Mayor - Word MVP
My web site www.gmayor.com Word MVP web site http://word.mvps.org <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
> I need to change the merge fields in a number of documents. For > example one of the old merge fields is: [quoted text clipped - 13 lines] > Many thanks, > Andrew Microsoft Newsserver - 06 Oct 2006 07:53 GMT Thanks very much Graham I'll check this out.
Andrew
> Replace will work on field codes provided they are toggled to display the > construction (ALT+F9) [quoted text clipped - 20 lines] >> Many thanks, >> Andrew Microsoft Newsserver - 08 Oct 2006 12:22 GMT Hi Graham.
I've been looking at the site for the batch replace which I would like to take advantage of as I need to update the syntax of the merge fields in a large number of documents. Do you know of a way to, using the batch replace, to toggle (equivalent to pressing Alt+F9) and also to update each document so that it displays the new merge fields if you toggle back?
Kind regards, Andrew
> Replace will work on field codes provided they are toggled to display the > construction (ALT+F9) [quoted text clipped - 20 lines] >> Many thanks, >> Andrew Graham Mayor - 08 Oct 2006 13:22 GMT Add the marked sections to the following part of the code in the macro Public Sub BatchReplaceAnywhere.
'Open each file and make the replacement Set MyDoc = Documents.Open(PathToUse & myFile) '*************************************** ActiveWindow.View.ShowFieldCodes = True '*************************************** ' Fix the skipped blank Header/Footer problem MakeHFValid ' Iterate through all story types in the current document For Each rngstory In ActiveDocument.StoryRanges ' Iterate through all linked stories Do SearchAndReplaceInStory rngstory, findText, Replacement ' Get next linked story (if any) Set rngstory = rngstory.NextStoryRange Loop Until rngstory Is Nothing Next '*************************************** ActiveWindow.View.ShowFieldCodes = False ActiveDocument.PrintPreview ActiveDocument.ClosePrintPreview '*************************************** 'Close the file, saving the changes. MyDoc.Close SaveChanges:=wdSaveChanges myFile = Dir$() Wend
May I suggest that when you run it that you try it on a folder containing copies of your documents and that you add the field type to the search to reduce the chance of similar words being picked up from the text eg Search for Mergefield fieldname1 replace with Mergefield fieldname2
 Signature <>>< ><<> ><<> <>>< ><<> <>>< <>><<> Graham Mayor - Word MVP
My web site www.gmayor.com Word MVP web site http://word.mvps.org <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
> Hi Graham. > [quoted text clipped - 39 lines] >>> Many thanks, >>> Andrew Microsoft Newsserver - 09 Oct 2006 00:24 GMT Thanks very much Graham I've got that working now.
Just wondering, as the find/replace changes I need to make are fixed is there a way to include them in the macro so I don't get the dialog box. There are 4 find/replaces I need to do on each document and was hoping to do them all in one go if possible and not 4 separate ones.
Many thanks, Andrew
> Add the marked sections to the following part of the code in the macro > Public Sub BatchReplaceAnywhere. [quoted text clipped - 74 lines] >>>> Many thanks, >>>> Andrew Doug Robbins - Word MVP - 09 Oct 2006 04:45 GMT Use the following where A1 is the replacement for the field A, B1 for B, etc
Dim vFindText As Variant Dim sReplText As Variant Dim i As Long
vFindText = Array("A", "B", "C", D") sReplText = Array("A1", "B1", "C1", D1") With Selection.Find .Forward = True .Wrap = wdFindContinue .MatchWholeWord = True .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False .Format = True .MatchCase = True For i = LBound(vFindText) To UBound(vFindText) .Text = vFindText(i) .Replacement.Text = sReplText(i) .Replacement.Highlight = True .Execute replace:=wdReplaceAll Next i End With
 Signature Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my services on a paid consulting basis.
Doug Robbins - Word MVP
> Thanks very much Graham I've got that working now. > [quoted text clipped - 84 lines] >>>>> Many thanks, >>>>> Andrew Microsoft Newsserver - 10 Oct 2006 04:23 GMT Hi Doug,
Thanks for your reply. I"m trying to incorporate your changes into my macro but getting stumped - I haven't used these in a while and are trying to learn as I go. Here's my current macro that's working well showing the find/replace dialog boxes for the merge fields. I just can't work out where to insert your suggestions - are you able to let me know the appropriate place to make your modifications?
Many thanks, Andrew
-------------------
Public Sub BatchReplaceAnywhere()
'Macro by Doug Robbins - 1st March 2004 'with additional input from Peter Hewett 'to replace text in all the documents in a folder
Dim FirstLoop As Boolean Dim myFile As String Dim PathToUse As String Dim myDoc As Document Dim rngstory As Word.Range Dim FindText As String Dim Replacement As String
' Get the folder containing the files With Dialogs(wdDialogCopyFile) If .Display <> 0 Then PathToUse = .Directory Else MsgBox "Cancelled by User" Exit Sub End If End With
'Close any documents that may be open If Documents.Count > 0 Then Documents.Close Savechanges:=wdPromptToSaveChanges End If
FirstLoop = True
If Left(PathToUse, 1) = Chr(34) Then PathToUse = Mid(PathToUse, 2, Len(PathToUse) - 2) End If
myFile = Dir$(PathToUse & "*.doc")
While myFile <> "" 'Get the text to be replaced and the replacement If FirstLoop = True Then FindText = InputBox("Enter the text that you want to replace.", "Batch Replace Anywhere") If FindText = "" Then MsgBox "Cancelled by User" Exit Sub End If Tryagain: Replacement = InputBox("Enter the replacement text.", "Batch ReplaceAnywhere ") If Replacement = "" Then Response = MsgBox("Do you just want to delete the found text?", vbYesNoCancel) If Response = vbNo Then GoTo Tryagain ElseIf Response = vbCancel Then MsgBox "Cancelled by User." Exit Sub End If End If FirstLoop = False End If
'Open each file and make the replacement Set myDoc = Documents.Open(PathToUse & myFile) ActiveWindow.View.ShowFieldCodes = True ' Fix the skipped blank Header/Footer problem MakeHFValid ' Iterate through all story types in the current document For Each rngstory In ActiveDocument.StoryRanges ' Iterate through all linked stories Do SearchAndReplaceInStory rngstory, FindText, Replacement ' Get next linked story (if any) Set rngstory = rngstory.NextStoryRange Loop Until rngstory Is Nothing Next ActiveWindow.View.ShowFieldCodes = False ActiveDocument.PrintPreview ActiveDocument.ClosePrintPreview 'Close the file, saving the changes. myDoc.Close Savechanges:=wdSaveChanges myFile = Dir$() Wend End Sub
Public Sub SearchAndReplaceInStory(ByVal rngstory As Word.Range, _ ByVal strSearch As String, _ ByVal strReplace As String) 'This routine supplied by Peter Hewett Do Until (rngstory Is Nothing) With rngstory.Find .ClearFormatting .Replacement.ClearFormatting .Text = strSearch .Replacement.Text = strReplace .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchAllWordForms = False .MatchSoundsLike = False .MatchWildcards = False .Execute Replace:=wdReplaceAll End With Set rngstory = rngstory.NextStoryRange Loop End Sub
Public Sub MakeHFValid() 'And this too Dim lngJunk As Long ' It does not matter whether we access the Headers or Footers property. ' The critical part is accessing the stories range object lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType End Sub
> Use the following where A1 is the replacement for the field A, B1 for B, > etc [quoted text clipped - 111 lines] >>>>>> Many thanks, >>>>>> Andrew Graham Mayor - 10 Oct 2006 09:19 GMT Replacing the main routine with the following should do the trick. Change the find and replace arrays to reflect the terms you wish to search and replace where indicated. Do not change the sub routines associated with this macro.
Public Sub BatchReplaceAnywhere()
Dim FirstLoop As Boolean Dim myFile As String Dim PathToUse As String Dim MyDoc As Document Dim rngstory As Word.Range Dim findText As Variant Dim Replacement As Variant Dim i As Long
With Dialogs(wdDialogCopyFile) If .Display <> 0 Then PathToUse = .Directory Else MsgBox "Cancelled by User" Exit Sub End If End With If Documents.Count > 0 Then Documents.Close SaveChanges:=wdPromptToSaveChanges End If FirstLoop = True If Left(PathToUse, 1) = Chr(34) Then PathToUse = Mid(PathToUse, 2, Len(PathToUse) - 2) End If myFile = Dir$(PathToUse & "*.doc") While myFile <> "" If FirstLoop = True Then '**************************************** findText = Array("Lorem", "ipsum", "dolor") '**************************************** End If TryAgain: '**************************************** Replacement = Array("WORD1", "WORD2", "WORD3") '**************************************** FirstLoop = False Set MyDoc = Documents.Open(PathToUse & myFile) MakeHFValid For Each rngstory In ActiveDocument.StoryRanges Do With Selection.Find For i = LBound(findText) To UBound(Replacement) .Text = findText(i) .Replacement.Text = Replacement(i) SearchAndReplaceInStory rngstory, findText(i), Replacement(i) Next i End With Set rngstory = rngstory.NextStoryRange Loop Until rngstory Is Nothing Next MyDoc.Close SaveChanges:=wdSaveChanges myFile = Dir$() Wend End Sub
> Hi Doug, > [quoted text clipped - 256 lines] >>>>>>> possible? Many thanks, >>>>>>> Andrew Graham Mayor - 10 Oct 2006 09:29 GMT Plan B I was forgetting these were fields you were changing. You'll need a couple of extra lines.
Public Sub BatchReplaceAnywhere()
Dim FirstLoop As Boolean Dim myFile As String Dim PathToUse As String Dim MyDoc As Document Dim rngstory As Word.Range Dim findText As Variant Dim Replacement As Variant Dim i As Long
With Dialogs(wdDialogCopyFile) If .Display <> 0 Then PathToUse = .Directory Else MsgBox "Cancelled by User" Exit Sub End If End With
If Documents.Count > 0 Then Documents.Close SaveChanges:=wdPromptToSaveChanges End If
FirstLoop = True
If Left(PathToUse, 1) = Chr(34) Then PathToUse = Mid(PathToUse, 2, Len(PathToUse) - 2) End If
myFile = Dir$(PathToUse & "*.doc")
While myFile <> "" If FirstLoop = True Then findText = Array("MERGEFIELD Name", "Mergefield Address", "ipsum", "dolor") End If TryAgain: Replacement = Array("MERGEFIELD First_Name", "Mergefield Postal_Address", "WORD2", "WORD3") FirstLoop = False Set MyDoc = Documents.Open(PathToUse & myFile) ActiveWindow.View.ShowFieldCodes = True MakeHFValid For Each rngstory In ActiveDocument.StoryRanges Do With Selection.Find For i = LBound(findText) To UBound(Replacement) .Text = findText(i) .Replacement.Text = Replacement(i) SearchAndReplaceInStory rngstory, findText(i), Replacement(i) Next i End With Set rngstory = rngstory.NextStoryRange Loop Until rngstory Is Nothing Next ActiveDocument.PrintPreview ActiveDocument.ClosePrintPreview ActiveWindow.View.ShowFieldCodes = False MyDoc.Close SaveChanges:=wdSaveChanges myFile = Dir$() Wend End Sub
> Hi Doug, > [quoted text clipped - 256 lines] >>>>>>> possible? Many thanks, >>>>>>> Andrew Lüko Willms - 06 Oct 2006 18:43 GMT Am Fri, 6 Oct 2006 05:58:27 UTC, schrieb "Microsoft Newsserver" <nospam@nospam.com> auf microsoft.public.word.mailmerge.fields :
> I need to change the merge fields in a number of documents. For example one > of the old merge fields is: [quoted text clipped - 4 lines] > > «dear» Editing the text of the documents is not feasable, you need to get access to the fields in a programmed way.
Loop thru the ActiveDocument.Mailmerge.MailMergeFields collection, and add the fields with the new names, when you encounter a field with the old name. The MailMergeFields collection does not provide means to delete a field, as far as I can tell. You may have to make the field with the old name to the current Selection, and then use the .Delete method to delete this selection.
Yours, L.W.
Graham Mayor - 07 Oct 2006 07:04 GMT Did you see my earlier reply? Of course you can edit fields in documents!
 Signature <>>< ><<> ><<> <>>< ><<> <>>< <>><<> Graham Mayor - Word MVP
My web site www.gmayor.com Word MVP web site http://word.mvps.org <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
> Am Fri, 6 Oct 2006 05:58:27 UTC, schrieb "Microsoft Newsserver" > <nospam@nospam.com> auf microsoft.public.word.mailmerge.fields : [quoted text clipped - 20 lines] > Yours, > L.W. Lüko Willms - 07 Oct 2006 08:55 GMT Am Sat, 7 Oct 2006 06:04:47 UTC, schrieb "Graham Mayor" <gmayor@REMOVETHISmvps.org> auf microsoft.public.word.mailmerge.fields :
> Did you see my earlier reply? I did.
> Of course you can edit fields in documents! Well, my experience told me otherwise, but maybe it is too limited.
BTW, what is the difference between SHIFT-F9 and ALT-F9?
BTW 2: one should not forget the "ActiveDocument.Fields.Update" after changing them, otherwise they will display the old name -- at least the way I did it.
Yours,
Doug Robbins - Word MVP - 07 Oct 2006 09:33 GMT Shift+F9 toggles the display of the field codes for the fields that are selected.
Alt+F9 toggles the display of all of the fields in the document.
 Signature Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my services on a paid consulting basis.
Doug Robbins - Word MVP
> Am Sat, 7 Oct 2006 06:04:47 UTC, schrieb "Graham Mayor" > <gmayor@REMOVETHISmvps.org> auf [quoted text clipped - 15 lines] > > Yours,
|
|
|