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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MS Word - need macro to highlight all mail merge fields wherever they are in a file

Status
Not open for further replies.

kmw75

MIS
Jun 11, 2014
9
Hi Everyone, I am looking for help with macro code. Need a macro to select all merge fields on a document wherever they are (and they wont all be near each-other) Basically I have legal documents and most text can not be edited because it has to be written a certain way. However I run a merge with a legal software app and need those fields to be editable only. I recorded a macro to do this for a one page document where there is one merge field. However I have about 100 other docs that the format is not that simple. I can not seem to select several selections using my mouse and I am not good at writing code unfortunately. I have documents with pages and pages of merge codes and not all in the same order. The one page document with the field code I recorded a macro for works exactly how we need. Next step for me is this request, selecting all merge codes in a document no matter where the fields are. Any help would be greatly appreciated! Thanks a lot! Kim
 
Hi,

MailMerge fields are not editable.

FormFields are editable.

Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]
 
Thanks Skip. Ill try and be clearer. I have a many documents that have fields linked from software called ProLaw. Our issue is that once the merge completes the entire document is editable. I am able to make a document un-editable except for a specific selection (or selections). This is what we need so that legal wording is not accidentally edited. I tested this feature and I recorded a macro to go down a few lines, highlight two lines and then restrict the edit features. This is exactly what I need for this one document that has the Codes (for example) on line 8 and 9. Now what I ultimately need is the macro to select all Field Codes in the file no matter where they are and/or no matter how many there are so that the last part of the macro will run and make only the field codes available for our clerical staff to edit. (The last part of the macro after selection does this: review menu, restrict editing, 1 is not checked, 2 is checked, no changes (Read only) is chosen, exception would be the selected fields I need selected, and everyone box is checked and then locked with a password.) Hope that helped, thanks again.
 
What I'm saying is that there are "MailMerge" fields and there are "FormFields" and the MailMerge fields are not editable. FormFields are. In fact the document is NOT editable other than FormFields, which is what you seem to want.

Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]
 
So if you use FormFields, the editing of the document is locked except fot the FormFields when it is prepared for users to use. Ive used this inthe past to "fill in the blanks" for real estate contracts.

Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]
 
Hi Skip, do form fields link with other applications to auto-fill data or are they mainly manual data entry? All of our word docs pull data from the legal software and the client record which the clerical staff is using at that moment. No manual entries are really needed the way the docs are but the editing factor leaves room for errors. The clerical staff when they are looking at a client, click a document title, word opens and populates from that clients record and then automatically attaches to the client record (when the staff member hits save )where the docs can be printed and kept.

Are you suggesting that turning all of our documents into forms is easier then coming up with a macro that selects all the existing fields? Never had to use this Legal software before this is new to me (New Job) so just trying to understand. Thanks again.
 
Well what method is being used in Word to "link" the legal software?

Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]
 
ProLaw actually calls Word, I believe with OLE, when I create a new document type, Word is in the list of available apps to merge with, it will launch Word when you select a Word document and you can create a document and add fields as if it was doing an official Mail Merge. Once you save the word doc, you can "call" it using the client data when you are in a user record and then it automatically mail merges. Its really quite fast and when done your document is complete, users click print and save and its copied into ProLaw. Over the years there are so many documents that have been created over several areas of law. The ProLaw application will allow a Macro to be called when the document is run. If I try to protect the whole document, the merge will not run, says the file is locked for editing (which it is). The only way I found to prevent this Lock is to run this little recorded macro, the macro seems to run after the merge completes, which is perfect, however; I am not even near beginner level of proficiency with VBA to use the correct code to do what I need. Also very hard to find this online anywhere so I had to try a forum...
 
Oh, so it uses the MailMerge feature. That's what the legal software employs. So that's what you're stuck with.

Here's the rub. You have a MailMerge document that has the MailMerge fields embedded. When the merge is completed, it spins off a separate document populated with the merge data from your software database, and those MergeFields disappear. It's this NEW DOCUMENT that needs to be protected, not the MailMerge document.

Am I correct?

Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]
 
The ProLaw application will allow a Macro to be called when the document is run.

So did you use some macro? IF so please post it here.



Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]
 
This was the simple one I recorded.

Sub ProLawTest1()
'
' ProLawTest1 Macro
'
'
Selection.MoveDown Unit:=wdLine, Count:=5
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Editors.Add wdEditorEveryone
ActiveDocument.Protect Password:="NoChange", NoReset:=False, Type:= _
wdAllowOnlyReading, UseIRM:=False, EnforceStyleLock:=False
Selection.HomeKey Unit:=wdStory
End Sub
 
Run the macro AFTER the new document has been created
Code:
Sub ProLawTest1()
 '
 ' ProLawTest1 Macro
 '
 '
    Selection.HomeKey Unit:=wdStory
    Selection.Editors.Add wdEditorEveryone
    ActiveDocument.Protect _
        Password:="NoChange", _
        NoReset:=False, _
        Type:=wdAllowOnlyReading, _
        UseIRM:=False, _
        EnforceStyleLock:=False
 End Sub


Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]
 
Thanks Skip, just tried it but it only locks the entire document. There may be a section or field which they will need to edit. Still hunting for the select all merge field code. Truly appreciate all your help!
 
But in THAT document, there are no MergeFields and no way to identify what data was inserted by any MergeField from the merge document!!!

Could you format each MergeField with UNDERLINE? Then the data that is merged would be underlined.

You might be able to do something like this in the MailMerge document...
Code:
Dim i As Integer

For i = 1 to ActiveDocument.MailMergeFields.Count
   ActiveDocument.MailMerge.Fields(i).Range.Text.Underline = True
Next
...then run the merge and see if the underline occurs in the new document.

I posted this from my iPad. I'll need to check if this works, on me laptop later.

Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]
 
Since formfields don't survive a mailmerge, adding them to the mailmerge main document won't help. Content controls, however, will survive a mailmerge. So, wherever you want the user to be able to edit the content, insert that content into a content control. Note that you can use a dropdown content control to limit user options, too. If the results of a merge field are to be editable, simply insert the mergefield into a content control. Then, once the merge has executed, apply the forms protection. Done.

Cheers
Paul Edstein
[MS MVP - Word]
 
This is tested and underlines each MailMerge field in the MailMerge Master document.

Code:
Sub UnderlineMailMerge()
    Dim fld As MailMergeField

    For Each fld In ActiveDocument.MailMerge.Fields
        fld.Code.Underline = wdUnderlineSingle
    Next
End Sub

Then the next thing would be to write code to condition each new document to protect the text that is not underlined. Don't have time to look at that tonight.

Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]
 
Thank you both! I will try your suggestions today!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top