Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 498
  • Last Modified:

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
0
codydoyle
Asked:
codydoyle
  • 4
  • 4
1 Solution
 
GrahamSkanRetiredCommented:
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

0
 
codydoyleAuthor Commented:
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....
0
 
GrahamSkanRetiredCommented:
Open the VBA IDE with Alt-F11.
If you have a module listed in the Normal project, double-click to open it, otherwise create one (Insert/Module).
Copy the macro into the code pane. Click in the code to select it the macro, and press f5 to run it. Alternatively you can press f8 to step throught th code.

This is a debug version. It reports the field count and highlights the fields in the document when found.

Sub ReplaceMergeFieldsDebug()
Dim fld As Field
Dim rng As Range
Dim strFldname As String
Dim MergefieldCount
MsgBox "There are " & ActiveDocument.Fields.Count & " fields in " & ActiveDocument.Name
For Each fld In ActiveDocument.Fields
    If fld.Type = wdFieldMergeField Then
        MergefieldCount = MergefieldCount + 1
        Set rng = ActiveDocument.GoTo(wdGoToField, , fld.Index)
        rng.Select ' make range visible
        strFldname = Mid$(fld.Result, 2, Len(fld.Result) - 2)
        fld.Delete
        ActiveDocument.Bookmarks.Add strFldname, rng
    End If
Next fld
MsgBox "There are now " & ActiveDocument.Fields.Count & " fields in " & ActiveDocument.Name & vbCrLf & MergefieldCount & " MergeFields were found and replaced with bookmarks."
End Sub



0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
codydoyleAuthor Commented:
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
0
 
GrahamSkanRetiredCommented:
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?
0
 
codydoyleAuthor Commented:
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.
0
 
GrahamSkanRetiredCommented:
Do you do a mailmerge?
0
 
codydoyleAuthor Commented:
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
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now