Hi, I have created a label template in Word and written a macro to print all records (see code below printAllRecords() and attachments) from an Excel datasource. The problem is when I press F2 (defined function key to print all records in my macro), it prints the 1st page once and then print all records (i.e. 1st page is printed twice). Anyone knows why and how to fix it?? Thanks!

Sub autoOpen() 

    Dim actPath As String 
    Dim strFileExcel As String 
    actPath = ActiveDocument.Path 
    strFileExcel = actPath + "\CCS Automation Template.xls" 
     ' Get the source and update labels
    ActiveDocument.MailMerge.OpenDataSource Name:= _ 
    strFileExcel, _ 
    ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=True, _ 
    AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", _ 
    WritePasswordDocument:="", WritePasswordTemplate:="", Revert:=False, _ 
    Format:=wdOpenFormatAuto, Connection:= _ 
    "Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=strFileExcel;Mode=Read;Extended Properties=""HDR=YES;IMEX=1;"";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Engine Type=35;J" _ 
    , SQLStatement:="SELECT * FROM `Consolidate$`", SQLStatement1:="", _ 
    ActiveDocument.MailMerge.ViewMailMergeFieldCodes = False 
    With Application 
         '// Refer to THIS document for customisations
        .CustomizationContext = ThisDocument 

         '// Add keybinding: F2
        .KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyF2), _ 
        KeyCategory:=wdKeyCategoryCommand, _ 
    End With 
    MsgBox "Press F2 button to print all records.", vbOKOnly, "Reminder" 

End Sub 

Sub printAllRecords() 
     ' Print all records in mail merge

    Dim bPrintBackgroud As Boolean 

     'Disable to display all the alerts
    bPrintBackgroud = Options.PrintBackground 
    Options.PrintBackground = False 
    Application.DisplayAlerts = wdAlertsNone 

     'Show the Print dialog box
    If Dialogs(wdDialogFilePrint).Show <> -1 Then End 
     'Print all records
    With ActiveDocument.MailMerge 
        .Destination = wdSendToPrinter 
        .SuppressBlankLines = True 
        With .DataSource 
            .FirstRecord = wdDefaultFirstRecord 
            .LastRecord = wdDefaultLastRecord 
        End With 
        .Execute Pause:=False 
    End With 

     'Restore all the alerts
    Application.DisplayAlerts = wdAlertsAll 
    Options.PrintBackground = bPrintBackgroud 

End Sub 

You can use something like -

ActiveWindow.SelectedSheets.PrintOut Copies:=1 ''Or as many pages as you need...
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.