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!

Excel VBA mail merge to Word table?

Status
Not open for further replies.

knifey

Technical User
Nov 14, 2006
180
GB

Hi,
I'm a newbie when it comes to Word VBA and am trying to use something along the lines of the code below to do a mailmerge from Excel to a Word template containing multiple tables that contain merge fields.

Code:
endRowshtName = ActiveWorkbook.Worksheets("ns1").Range("A65536").End(xlUp).Row
endColshtName = ActiveWorkbook.Worksheets("ns1").Range("IV1").End(xlToLeft).Column
endColLettershtName = colNo2ColRef(endColshtName)
'Set shtNameRange = wb1.Worksheets("ns1").Rows("2:" & endRowshtName).Columns("A:" & endColLettershtName).Cells
wordTemplateFname = "C:\Users\Roy\Documents\Sandpit\Word Templates\Status_Report_Template 3.dotx"
ActiveDocument.MailMerge.MainDocumentType = wdDirectory
ActiveDocument.MailMerge.OpenDataSource Name:=ThisWorkbook.Path & "\" & ThisWorkbook.Name '& "!" & shtNameRange
With ActiveDocument.MailMerge
    .Destination = wdSendToNewDocument
    .SuppressBlankLines = True
    With .DataSource
        .FirstRecord = wdDefaultFirstRecord
        .LastRecord = endRowshtName
    End With
    .Execute Pause:=False
End With

This is currently creating multiple tables of the merge data instead of filling the rows of a specific table with the merge data. Is there any way for me to reference the Word table name and the MailMerge.OpenDataSource range to make this work? Or is populating Word tables beyond the limits of the mailmerge method? And if yes, what would be a good workaround?
Any help and advice would be much appreciated.
Thanks,
Roy

 
hi,

Can you do it manually in Word?

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Hi Skip,
It appears not, but I'm no mail merge expert. If this isn't possible using the mailmerge method can you advise any sort of workaround (elegant or not).
Maybe using a for each row loop which creates a new line in the word table before adding the data or a copy and paste? These seem very clunky but I'm getting desperate.

Thanks,
Roy

 
If you have a situation where each row in your Word table is a different row in the source data, then use the CATALOGUE option, rather than the LETTER option.

This will put a {NEXTRECORD} on each row to advance the cursor.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top