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

Copy the Mergefield content to a VBA variable

Status
Not open for further replies.

Roppie

Programmer
Oct 25, 2012
3
NL
I have a Excel sheet filled with data. I copy some of this data by Mail merge to a Word document.
Then, I save this merged documents one by one. The name of the saved document is composed by some data from the Merged fields like:
lblDocName = ActiveDocument.Fields(60).Result
No problem so far.
Sometimes the index number is change as example from 60 to 59. This can happen when I transfer the documents to another computer.
How can i use the field name instead of the index number?
I have tried:
lblDocName = ActiveDocument.Fields("DocName").Result
But, the field content looks like this: " MERGEFIELD "DocName"
Sorry for my English skills.

Rob
 
And this ?
lblDocName = ActiveDocument.Fields("DocName").Result.Text

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
How is Dim'ed lblDocName ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Yes, that was the hint I needed. Problem solved.
Many thanks for your effort.

Rob
 
Why are you trying to read the mergefield, rather than the data field for the record concerned? Since you're using vba, you may as well get the string from the data directly, rather than relying on the rather roundabout approach of getting it into the document then reading it from there.

Cheers
Paul Edstein
[MS MVP - Word]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top