Programmatically expose names of the "Merge fields in document"

L

Lee C.

I see the "Merge fields in document" listed in the document's System
Information | Mail Merge Item Values, but I cannot figure out how to
expose them in code. I have tried:
.DataSource.FieldNames.Count 'gives count of total available fields
.DataSource.DataFields.Count 'also gives count of total available
fields
.DataSource.MappedDataFields.Count 'not even close to what I want,
but I tried it anyway.

Can someone tell me the name of the DataSource property?

Thanks,
Lee
 
P

Peter Jamieson

You could start with something like the following, which inspects /all/
field types. I wouldn't guarantee that it will find them all but it's the
nearest I've ever got.

Sub IterateMergeFields()
Dim r As Range
Dim f As Field
Dim s As Shape
For Each r In ActiveDocument.StoryRanges
If r.StoryType <> wdTextFrameStory Then
For Each f In r.Fields
If f.Type = wdFieldMergeField Then
' do whatever you need
End If
Next
End If
Next
For Each s In ActiveDocument.Shapes
If s.Type = msoTextBox Then
For Each f In s.TextFrame.TextRange.Fields
If f.Type = wdFieldMergeField Then
' do whatever you need
End If
Next
End If
Next
End Sub
 
K

Kathryn Morgand

Thanks. I was trying another angle, but I'm confused now. Is that info
that I see in the document's System Information stored in the document's
Compound Document properties?

, Lee
 
P

Peter Jamieson

Thanks. I was trying another angle, but I'm confused now. Is that info
that I see in the document's System Information stored in the document's
Compound Document properties?

I don't think it is stored anywhere. As far as I know the System Information
facility is just a program that collects various bits of information about
the Windows environment and the current application and document using a
variety of techniques, presumably including document automation to collect
the info. about the mailmerge fields. But I don't know for sure.
 
K

Kathryn Morgand

Yeah, I've decided that it's not worth trying to figure it out for what I'm
doing. It just seemed like it should be so simple.

Anyway, I appreciate your insight, Mr. Jamieson.

Thanks again,
Lee
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top