View Single Post
 
Old 11-04-2024, 11:46 AM
WizKidd WizKidd is offline Windows 11 Office 2021
Novice
 
Join Date: Nov 2024
Posts: 1
WizKidd is on a distinguished road
Arrow Corrective Tweak

Quote:
Originally Posted by gmayor View Post
It is difficult to understand the point of this, but the following macro will work, provided you attach the datasource to the blank document:
Code:
Sub Macro1()
Dim i As Integer
Dim orng As Range
    With ActiveDocument.MailMerge.DataSource
        For i = 1 To .FieldNames.Count
            Set orng = ActiveDocument.Range
            orng.Collapse 0
            orng.Fields.Add orng, wdFieldMergeField, Chr(34) & .FieldNames(i), False
            Set orng = ActiveDocument.Range
            orng.Collapse 0
            orng.InsertParagraphAfter
        Next i
    End With
    Set orng = Nothing
End Sub
When I ran this, each field name had a space at the end, which didn't match my DataSource (e.g.: «NO_OF_DAYS ») so I removed just the "Chr(34) & " from line 8, then they came out correctly («NO_OF_DAYS»).

Thanks!

P.S.: WOW, I haven't used BBCode in ages!
Reply With Quote