Need some help manipulating { MERGEFIELD } and { FORMTEXT } fields...


I have a document that gets it data from a MailMerge.  It has many fields, but for the sake of this question, I will discuss just one field:

{ MERGEFIELD "Industry1" }

I also have a corresponding { FORMTEXT } field named "Industry1".

What I would like to do is the following:
1. Create a For Each...Next Loop that cycles through all of my { FORMTEXT } fields on my form, and if there is a { MERGEFIELD } by the same name, its sets the { FORMTEXT } field's .Result to the { MERGEFIELD } field's .Value.
2. To do this it will have to have some sort of checking to make sure that for the { FORMTEXT } Field it is currently checking, that there is a corresponding { MERGEFIELD }, by the same name.  If there is not, it should skip to the next { FORMTEXT } field, if there is, it should set the .Result of the { FORMTEXT } Field as described.

The point of this is to populate all of { FORMTEXT } Fields with the Merge Data wherever the names match and the data exists.

Thanks :)

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Is this what you mean?

Sub FillFormFields()
    Dim ffld As FormField
    Dim fld As Field
    For Each ffld In ActiveDocument.FormFields
        For Each fld In ActiveDocument.Fields
            If fld.Type = wdFieldMergeField Then
                If Chr$(34) & ffld.Name & Chr$(34) = Split(fld.Code, " ")(2) Then
                    ffld.Result = ActiveDocument.MailMerge.DataSource.DataFields(ffld.Name)
                End If
            End If
        Next fld
    Next ffld
End Sub

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MitchellVIIAuthor Commented:
Hey Graham,

The problem with that code is that it only works if the { MERGEFIELD }'s themselves are present on the document.  In this case, they are in the .datasource but not physically on the document, so it doesn't see them and my form fields don't get populated.  Also, it is really slow (takes like 10 seconds to run).  I think that is because for every .FormField it is checking every .Field, and that takes a long time.

What I have been using that is kind of a futz because it uses On Error Resume Next to by-pass when the fields don't match is:

On Error Resume Next

Dim fld As FormField

For Each fld In ThisDocument.FormFields
    ThisDocument.FormFields(fld.Name).Result = ThisDocument.MailMerge.DataSource.DataFields(fld.Name).Value

This takes only about 1/2 a second to work.

Actually that's a bit easier.

I don't like disabling error checking, either.

All you need is an inner loop to check the field names.

Sub FillFormFields2()
    Dim ffld As FormField
    Dim mmdf As MailMergeDataField

    For Each ffld In ActiveDocument.FormFields
        For Each mmdf In ActiveDocument.MailMerge.DataSource.DataFields
            If mmdf.Name = ffld.Name Then
                ffld.Result = mmdf.Value
            End If
        Next mmdf
    Next ffld
End Sub
MitchellVIIAuthor Commented:
Thanks Graham,

It works without the Error Checking futz, but it is still a few seconds slower.  I guess that blowing past Errors may result in some sort of build up up "ghost code" in my document eventually causing a corruption.  That may be paranoia, but Error Messages exist for a reason - overriding them always just seems scary to me.


P.S., I am still having trouble with the Colum Headings on my Combo's.  I started a new Post on that.  If you get a second, could you take a quick look?


It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.