Thread: [Solved] Macro to Print Custom Range
View Single Post
 
Old 05-11-2018, 03:31 AM
kiwimtnbkr kiwimtnbkr is offline Windows 10 Office 2010 64bit
Advanced Beginner
 
Join Date: Oct 2017
Posts: 69
kiwimtnbkr is on a distinguished road
Default Macro to Print Custom Range

Looking for some assistance with code that will allow selected pages of the document the attached code is part of to be printed. (code is courtesy of Allen Wyatt and his word.tip.net postings)

Sometimes it may be only page 85-86, other times it may be pages 3-4, 11-16, 61-80.

It would also be good that if a custom range isn't entered that the entire document is printed.

Code:
Sub PrintNumberedCopies()
    Dim varItem As Variable
    Dim bExists As Boolean
    Dim lCopiesToPrint As Long
    Dim lCounter As Long
    Dim lCopyNumFrom As Long

    ' ensure our doc variable exists
    bExists = False
    For Each varItem In ActiveDocument.Variables
        If varItem.Name = "CopyNum" Then
            bExists = True
            Exit For
        End If
    Next varItem

    ' initialize document variable if doesn't exist
    If Not bExists Then
        ActiveDocument.Variables.Add _
            Name:="CopyNum", Value:=0
    End If

    ' ask how many to print
    On Error GoTo Canceled
    lCopiesToPrint = InputBox( _
        Prompt:="How many copies do you require?", _
        Title:="Print and Number Copies", _
        Default:="1")

    ' ask where to start numbering
    On Error GoTo Canceled
    lCopyNumFrom = CLng(InputBox( _
        Prompt:="Number at which to start numbering copies?", _
        Title:="Print and Number Copies", _
        Default:=CStr(ActiveDocument.Variables("CopyNum") + 1)))

    ' loop through the print-write-print cycle
    For lCounter = 0 To lCopiesToPrint - 1
        ' update the document variable
        ActiveDocument.Variables("CopyNum") = _
            lCopyNumFrom + lCounter
                With Options
                    .UpdateFieldsAtPrint = True
                    .UpdateLinksAtPrint = True
                End With
                    ActiveDocument.Fields.Update
        ' print this numbered copy
        ActiveDocument.PrintOut Copies:=1
    Next lCounter
    
Canceled:
End Sub
Reply With Quote