I want to make a mail merge for labels.
I want it to run from my excel application.
I know already that it's better to open a word document that runs a word macro that makes the labels.
But instead, I need my excel macro creating a new word doc with the labels inside.
It works already fine, except to propagate the fields in every label. I tried it with
But apparently, when I open the word doc afterwards, it isn't filled with the fields, and I have to do it manually again myself.
Can someone help with this?
Or does someone have a working example?
I want it to run from my excel application.
I know already that it's better to open a word document that runs a word macro that makes the labels.
But instead, I need my excel macro creating a new word doc with the labels inside.
It works already fine, except to propagate the fields in every label. I tried it with
Code:
For c = 1 To .mailmerge.Fields.Count
.mailmerge.Fields.Add Range:=Selection.Range.Fields(c), Type:=wdFieldMergeField, Text:="""Naam"""
.mailmerge.Fields.Update
Next c
Can someone help with this?
Or does someone have a working example?
Code:
Sub mailmerge()
MsgBox "mailmerge is working"
Dim appWD As Object
Dim etiketten As String
etiketten = "C:\myfolder\mylabels.doc"
Dim strThisWorkbook As String
strThisWorkbook = ThisWorkbook.FullName
If appWD Is Nothing Then
Set appWD = CreateObject("Word.Application")
End If
appWD.Visible = True
Set wrdDoc = appWD.Documents.Add
With wrdDoc
.SaveAs (etiketten)
.Activate
End With
With wrdDoc
.mailmerge.MainDocumentType = wdMailingLabels
.mailmerge.OpenDataSource ThisWorkbook.FullName, , , , , , , , , , , "Data Source=" & strThisWorkbook & ";Mode=Read", "SELECT * FROM `Temp$`"
.mailmerge.ViewMailMergeFieldCodes = wdToggle
' somewhere here, the script does not propagate the fields in the word document
For c = 1 To .mailmerge.Fields.Count
.mailmerge.Fields.Add Range:=Selection.Range.Fields(c), Type:=wdFieldMergeField, Text:="""Naam"""
.mailmerge.Fields.Update
Next c
End With
With wrdDoc
.SaveAs (etiketten)
End With
Set wrdDoc = Nothing
appWD.Quit
Set appWD = Nothing
End Sub