Learn how to a build a cloud-first strategyRegister Now

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

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

Hi,

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

Mitchell
0
MitchellVII
Asked:
MitchellVII
  • 2
  • 2
1 Solution
 
GrahamSkanCommented:
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

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

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

M
0
 
GrahamSkanCommented:
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
0
 
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.

Mitchell

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?

Thanks,

M
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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