Reply
 
Thread Tools Display Modes
  #1   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Microsoft Newsserver Microsoft Newsserver is offline
external usenet poster
 
Posts: 13
Default Changing Merge Field Names

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


  #2   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Graham Mayor Graham Mayor is offline
external usenet poster
 
Posts: 19,312
Default Changing Merge Field Names

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.

--

Graham Mayor - Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org



Microsoft Newsserver wrote:
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



  #3   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Microsoft Newsserver Microsoft Newsserver is offline
external usenet poster
 
Posts: 13
Default Changing Merge Field Names

Thanks very much Graham I'll check this out.

Andrew


"Graham Mayor" wrote in message
...
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.

--

Graham Mayor - Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org



Microsoft Newsserver wrote:
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





  #4   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Lüko Willms Lüko Willms is offline
external usenet poster
 
Posts: 13
Default Changing Merge Field Names

Am Fri, 6 Oct 2006 05:58:27 UTC, schrieb "Microsoft Newsserver"
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:

«Contactsdear»

The new merge field is:

«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.

  #5   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Graham Mayor Graham Mayor is offline
external usenet poster
 
Posts: 19,312
Default Changing Merge Field Names

Did you see my earlier reply? Of course you can edit fields in documents!

--

Graham Mayor - Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org


Lüko Willms wrote:
Am Fri, 6 Oct 2006 05:58:27 UTC, schrieb "Microsoft Newsserver"
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:

«Contactsdear»

The new merge field is:

«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.





  #6   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Lüko Willms Lüko Willms is offline
external usenet poster
 
Posts: 13
Default Changing Merge Field Names

Am Sat, 7 Oct 2006 06:04:47 UTC, schrieb "Graham Mayor"
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,
  #7   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Doug Robbins - Word MVP Doug Robbins - Word MVP is offline
external usenet poster
 
Posts: 8,832
Default Changing Merge Field Names

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.

--
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

"Lüko Willms" wrote in message
...
Am Sat, 7 Oct 2006 06:04:47 UTC, schrieb "Graham Mayor"
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,



  #8   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Microsoft Newsserver Microsoft Newsserver is offline
external usenet poster
 
Posts: 13
Default Changing Merge Field Names

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


"Graham Mayor" wrote in message
...
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.

--

Graham Mayor - Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org



Microsoft Newsserver wrote:
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





  #9   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Graham Mayor Graham Mayor is offline
external usenet poster
 
Posts: 19,312
Default Changing Merge Field Names

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


--

Graham Mayor - Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org



Microsoft Newsserver wrote:
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


"Graham Mayor" wrote in message
...
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.

--

Graham Mayor - Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org



Microsoft Newsserver wrote:
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



  #10   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Microsoft Newsserver Microsoft Newsserver is offline
external usenet poster
 
Posts: 13
Default Changing Merge Field Names

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



"Graham Mayor" wrote in message
...
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


--

Graham Mayor - Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org



Microsoft Newsserver wrote:
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


"Graham Mayor" wrote in message
...
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.

--

Graham Mayor - Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org



Microsoft Newsserver wrote:
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







  #11   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Doug Robbins - Word MVP Doug Robbins - Word MVP is offline
external usenet poster
 
Posts: 8,832
Default Changing Merge Field Names

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


--
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

"Microsoft Newsserver" wrote in message
...
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



"Graham Mayor" wrote in message
...
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


--

Graham Mayor - Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org



Microsoft Newsserver wrote:
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


"Graham Mayor" wrote in message
...
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.

--

Graham Mayor - Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org



Microsoft Newsserver wrote:
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







  #12   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Microsoft Newsserver Microsoft Newsserver is offline
external usenet poster
 
Posts: 13
Default Changing Merge Field Names

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.StoryT ype
End Sub




"Doug Robbins - Word MVP" wrote in message
...
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


--
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

"Microsoft Newsserver" wrote in message
...
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



"Graham Mayor" wrote in message
...
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


--

Graham Mayor - Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org



Microsoft Newsserver wrote:
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


"Graham Mayor" wrote in message
...
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.

--

Graham Mayor - Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org



Microsoft Newsserver wrote:
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








  #13   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Graham Mayor Graham Mayor is offline
external usenet poster
 
Posts: 19,312
Default Changing Merge Field Names

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




Microsoft Newsserver wrote:
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.StoryT ype
End Sub




"Doug Robbins - Word MVP" wrote in message
...
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


--
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

"Microsoft Newsserver" wrote in message
...
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



"Graham Mayor" wrote in message
...
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


--

Graham Mayor - Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org



Microsoft Newsserver wrote:
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


"Graham Mayor" wrote in message
...
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.

--

Graham Mayor - Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org



Microsoft Newsserver wrote:
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



  #14   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Graham Mayor Graham Mayor is offline
external usenet poster
 
Posts: 19,312
Default Changing Merge Field Names

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



Microsoft Newsserver wrote:
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.StoryT ype
End Sub




"Doug Robbins - Word MVP" wrote in message
...
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


--
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

"Microsoft Newsserver" wrote in message
...
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



"Graham Mayor" wrote in message
...
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


--

Graham Mayor - Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org



Microsoft Newsserver wrote:
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


"Graham Mayor" wrote in message
...
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.

--

Graham Mayor - Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org



Microsoft Newsserver wrote:
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



Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Word: IF statement to merge a field only if another Field is popul HelenC Mailmerge 3 August 24th 06 10:33 AM
Database Query on Merge field KarenB Mailmerge 7 April 3rd 06 05:34 PM
Word merge field names vs. Excel merge field names [email protected] Mailmerge 5 November 11th 05 05:24 PM
Merge Field Names showing after Document Converted John Shaening Mailmerge 1 August 10th 05 02:36 PM
Includetext Merge Field Jan Mailmerge 1 April 19th 05 01:36 AM


All times are GMT +1. The time now is 10:44 PM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"