Excel to Word mail merge for labels (formatting)

boothrat

New Member
Joined
Feb 4, 2014
Messages
13
I'm writing a macro to simplify the merging of labels from Excel to Word, I've got the code below but I need to adjust the font size, bold or italic some of the merge fields, add some text and would like to have the macro select 30per sheet labels at the start every time and always use as range named aa_Loom_Labels as the data source. I've added notes to the code where I'm trying to achieve the formatting, some assistance would be awesome!


Code:
Sub aaa_Label_Merge()


Dim oWord As Object
Dim oDoc As Object
Dim sPath As String
Dim oTable As Object
Dim oRng As Object
Dim oCell As Object
Dim i As Long


    sPath = ThisWorkbook.FullName
    Set oWord = CreateObject("Word.Application")
    Set oDoc = oWord.Documents.Add
    oWord.Visible = True
    oDoc.Activate
    oDoc.MailMerge.MainDocumentType = 1
    oWord.Dialogs(1367).Show
'Always select 30per sheet labels as the type
    Set oTable = oDoc.Tables(1)
    Set oRng = oTable.Cell(1, 1).Range
    oRng.End = oRng.End - 1
    
    With oDoc.MailMerge.Fields
'this merge field needs to be bold and 18pt font
        .Add oRng, "LABEL"
        oRng.Start = oTable.Cell(1, 1).Range.End - 1
        oRng.InsertParagraph
        oRng.Collapse 0
'this merge field needs a text label "LOCATION:" inserted before it
        .Add oRng, "LOCATION"
        oRng.Start = oTable.Cell(1, 1).Range.End - 1
        oRng.InsertParagraph
        oRng.Collapse 0
'the remaining merge fields need to be all on the same line and italics
        .Add oRng, "TYPE"
        oRng.Start = oTable.Cell(1, 1).Range.End - 1
        oRng.InsertParagraph
        oRng.Collapse 0
        .Add oRng, "CABLE"
        oRng.Start = oTable.Cell(1, 1).Range.End - 1
        oRng.InsertParagraph
        oRng.Collapse 0
        .Add oRng, "LENGTH"
        oRng.Start = oTable.Cell(1, 1).Range.End - 1
        oRng.Text = "  "
    End With


    
    Set oRng = oTable.Cell(1, 1).Range
    oRng.End = oRng.End - 1
    For i = 2 To oTable.Range.Cells.Count
        If oTable.Range.Cells(i).Width = oTable.Cell(1, 1).Width And _
           oTable.Range.Cells(i).Height = oTable.Cell(1, 1).Height Then
            Set oCell = oTable.Range.Cells(i).Range
            oCell.End = oCell.End - 1
            oCell.FormattedText = oRng
            oCell.Collapse 1
            oCell.Fields.Add oCell, 41, , False
        End If
    Next i


    oDoc.MailMerge.OpenDataSource sPath
'always use range aa_Loom_Labels as the datasource
    oDoc.ActiveWindow.View.ShowFieldCodes = False
    oDoc.MailMerge.ViewMailMergeFieldCodes = False
    oDoc.MailMerge.Destination = 0
    oDoc.MailMerge.Execute Pause:=False
    'oWord.Quit False '- This will just clear the merge?
End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number

Forum statistics

Threads
1,223,275
Messages
6,171,119
Members
452,381
Latest member
Nova88

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