Use the data from a mergefield in a macro

Hello

With the help of forums and the Microsoft KB, I wrote a macro that selects a mergefield (eg. the column "Group"), gets his value (eg. "B2") and insert the content of the related text file (eg. "C:\B2.TXT").

My macro works well, except that the mail merge part doesn't work. It takes the value of the first line of my data source and replicates it on every record.

Here is a simplified version of my macro (of course, it would be useless if it only did that):

Sub ShowGroup

  Dim GroupVal As String

  GroupVal = ActiveDocument.MailMerge.DataSource.DataFields("Group").Value

  Selection.TypeText (GroupVal)

End Sub

Do you have an idea of how I should correct this macro, and also when I should run it?

Thanks in advance!

Patrick

Answer
Answer
You do not need a macro for that purpose.  Use an Includetext field in which you include the mergefield, along with the path to the file as the argument.
Hope this helps,
Doug Robbins - MVP Office Apps & Services (Word)
dougrobbinsmvp@gmail.com
It's time to replace ‘Diversity, Equity & Inclusion’ with ‘Excellence, Opportunity & Civility’ - V Ramaswamy

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated November 4, 2022 Views 1,161 Applies to: