how to propagate label fields in word Mail Merge from Excel

bulevardi

Board Regular
Joined
Apr 21, 2011
Messages
69
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
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
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?

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
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
When I reopen the word document later on aswel.
He doesn't open it as a labels type document, but as the default word document.
 
Upvote 0

Forum statistics

Threads
1,224,591
Messages
6,179,767
Members
452,940
Latest member
rootytrip

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top