Link to home
Start Free TrialLog in
Avatar of codydoyle
codydoyle

asked on

Replace existing mergefields in a Word template with book marks that have a new name

Hi,
Can anyone tell me if there is a quick way to do some sort of find and replace for a word template that contains mergefields and replace each field with a bookmark that will be populated with input from a userform?

Thanks
Cody
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Do you mean like this macro?

Sub ReplaceMergeFields()
Dim fld As Field
Dim rng As Range
Dim strFldname

For Each fld In ActiveDocument.Fields
    If fld.Type = wdFieldMergeField Then
        Set rng = ActiveDocument.GoTo(wdGoToField, , fld.Index)
        strFldname = Mid$(fld.Result, 2, Len(fld.Result) - 2)
        fld.Delete
        ActiveDocument.Bookmarks.Add strFldname, rng
    End If
Next fld
End Sub

Avatar of codydoyle
codydoyle

ASKER

Hello, thanks for the reply,
Hmmm.... did not seem to do anything, I am not very familiar with macro's so I may have done somthing wrong....
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Grahm,
The macro runs and works perfectly if insert a field manually!! But the fields are still in the document. Sorry if I am being a pain, but these fields are a little strange here is an example:

admAF 26B8D42FCBC61E47AD24549E63CA431E \* MERGEFORMAT

wonder if this could have somthing to do with it? There is no mergefeild at the beginning......
Thanks,
Cody
I don't understand the situation. Do you have mailmerge fields to replace, or are they some other sort of field? How do the get there? How similar are they to each other?
Honestly I have no idea what king of fields they are. I thought they were mergefields. Apparently they were created by some sort of word addin application, the author is no longer with us.....so I cant be for sure.
Do you do a mailmerge?
Sorry, found a bit of information, I am not familiar with mail merge. It appears these were created with the mergerec field....  the string in front of mergformat mus represent some type of record... If mailmerge uses mergerec, then that would most likely be what it is. Can this be done? ...... Tell you what, my initial question was for mergefield and you gave me the solution for that and it works and helps with half of the document. I dont think it would be considered point passing if I posted a new question for the exact code that I have. I will accept your answer for mergefields and post a new one.... thankyou