Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-11-2018, 03:31 AM
kiwimtnbkr kiwimtnbkr is offline Macro to Print Custom Range Windows 10 Macro to Print Custom Range Office 2010 64bit
Advanced Beginner
Macro to Print Custom Range
 
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
  #2  
Old 05-20-2018, 03:33 AM
kiwimtnbkr kiwimtnbkr is offline Macro to Print Custom Range Windows 10 Macro to Print Custom Range Office 2010 64bit
Advanced Beginner
Macro to Print Custom Range
 
Join Date: Oct 2017
Posts: 69
kiwimtnbkr is on a distinguished road
Default Print macro almost there but not quite...

I have most of the macro working as I intend it BUT I'm stuck on the third InputBox sequence insofar as I am not sure how to specify and code the page range that I require to be printed and then have that page range auto-populate the With Dialogs .page line. I have a 'default range' manually typed in at the moment for 'testing' purposes.

Question two is then how do I get the With Dialogs(wddialogfileprint) to auto cycle thru so I don't have to press OK every copy that I want printed?

Code:
Sub PrintNumberedCopiesSelectionofPages()
'
' PrintNumberedCopiesSelectionofPages Macro
' Shortcut Key Alt+S
'
Dim Msg As String, Ans As Variant
    Dim lCopiesToPrint As Long
    Dim lCounter As Long
    ' Dim lRange As Long    ' is this even correct for what I am trying to do?
 
     Ans = MsgBox(Space(20) & "Is the printer configured for 2-sided printing?" & vbCrLf & vbCrLf & _
        "If not then click No, click 'Properties', select '2-sided printing' and click 'OK'.", _
        vbMsgBoxSetForeground + vbQuestion + vbYesNo, (Space(50) & "ABCDF 1234"))
    
   Select Case Ans
    
        Case vbYes
    
        Case vbNo
        
         With Dialogs(wdDialogFilePrint)
         If .Show = 0 Then End
         
         End With
        
End Select

    ' ask how many to print
    On Error GoTo Canceled
    lCopiesToPrint = InputBox( _
        Prompt:="How many copies do you require?", _
        Title:=(Space(45) & "ABCDF 1234"), _
        Default:="1")
 
    ' ask where to start numbering
    On Error GoTo Canceled
    lCopyNumFrom = CLng(InputBox( _
        Prompt:="Number at which to start numbering copies?", _
        Title:=(Space(45) & "ABCDF 1234"), _
        Default:=CStr(ActiveDocument.Variables("CopyNum") + 1)))
        
    ' ask what pages need printing
      On Error GoTo Canceled
      lPageRange = CLng(InputBox( _
         Prompt:="What pages require printing?", _
         Title:=(Space(45) & "ABCDF 1234"), _
         Default:="1-4"))                     ' how do I specify the page range I want to print here and have it populate the With Dialogs .pages in the print-write-print cycle line below?
        
 
    ' 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 = False
                    .UpdateLinksAtPrint = True
                End With
                    ActiveDocument.Fields.Update
        ' print this numbered copy
         With Dialogs(wdDialogFilePrint)
         .Pages = "2-3"
         .Range = wdPrintRangeOfPages
         .Show
         End With
    Next lCounter       'this repeats the cycle as expected but how do I get the wddialogfileprint to auto cycle thru as well so I don't have to OK everything?  Comment out the "ask what pages need printing" code to see what I mean about auto-cycling thru
 
Canceled:
 
End Sub
Appreciate ANY help in getting this annoying last bit resolved.

cheers
Mike
Reply With Quote
  #3  
Old 05-22-2018, 01:38 AM
kiwimtnbkr kiwimtnbkr is offline Macro to Print Custom Range Windows 10 Macro to Print Custom Range Office 2010 64bit
Advanced Beginner
Macro to Print Custom Range
 
Join Date: Oct 2017
Posts: 69
kiwimtnbkr is on a distinguished road
Default

Well the answer ended up being surprisingly easy. Changes to the code are highlighted in red.

Code:
Sub PrintNumberedCopiesSelectionofPages()
'
' PrintNumberedCopiesSelectionofPages Macro
' Shortcut Key Alt+S
'
Dim Msg As String, Ans As Long
    Dim lCopiesToPrint As Long
    Dim lCounter As Long
    Dim strPages As String
     
      Ans = MsgBox(Space(1) & "Is the document print settings configured  for 'Print on Both Sides?" & vbCrLf & vbCrLf & _
         "If not then click 'No', click the 'File' tab, 'Print', select 'Print on  Both Sides / Flip pages on long edge' and then press 'Alt+S'.", _
        vbMsgBoxSetForeground + vbQuestion + vbYesNoCancel, (Space(50) & "ABCDF 1234"))
   
       Select Case Ans
             
        Case vbYes
        Case vbNo
        End
        Case vbCancel
        End
        
End Select

    ' ask how many to print
    On Error GoTo Canceled
    lCopiesToPrint = InputBox( _
        Prompt:="How many copies do you require?", _
        Title:=(Space(45) & "ABCDF 1234"), _
        Default:="1")
 
    ' ask where to start numbering
    On Error GoTo Canceled
    lCopyNumFrom = CLng(InputBox( _
        Prompt:="Number at which to start numbering copies?", _
        Title:=(Space(45) & "ABCDF 1234"), _
        Default:=CStr(ActiveDocument.Variables("CopyNum") + 1)))
        
    ' ask what pages need printing
      On Error GoTo Canceled
      strPages = InputBox( _
         Prompt:="What pages require printing?", _
         Title:=(Space(45) & "ABCDF 1234"), _
         Default:="1-4")        

    ' 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 = False
                    .UpdateLinksAtPrint = True
                End With
                    ActiveDocument.Fields.Update
        ' print this numbered copy
         ActiveDocument.PrintOut Range:=wdPrintRangeOfPages, Pages:=strPages
         
    Next lCounter
 
Canceled:
 
End Sub
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I print a range of pages? thelighthouse Word 3 03-22-2018 09:50 AM
Fail-safe way to print right page range? New Daddy Word 5 09-22-2013 11:10 PM
Macro to Print Custom Range Print Macro w/ Page Range? stickyit Outlook 1 10-06-2011 09:51 AM
Macro to Print Custom Range Generic range for custom sort macro? tswiers Excel Programming 2 08-11-2011 02:40 AM
Macro to Print Custom Range print range questions A2Irish Excel 1 05-05-2011 05:25 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:56 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft