![]() |
|
|
|
#1
|
|||
|
|||
|
Absolutely not a worry with the delay in your reply, was kind of expecting it - and the wait was worth it!
After a very minor change to three lines of your code (highlighted below) it most certainly works at home so massive thanks. Will try tomorrow at work and see what happens. Code:
Sub PrintNumberedCopiesEntireDocument()
'
' PrintNumberedCopiesEntireDocument Macro
' Shortcut keys Alt+E
'
Const cdp_name As String = "CopyNum"
Const default_copies As String = "1"
Const duplex_printer As String = "hp deskjet 5550 series (HPA) duplex" ' change this to the name of your duplex printer
Dim my_form As ufrmPrintNumberedCopies
Dim copy_number As Long
Dim first_copy As Long
Dim last_copy As Long
Dim my_update_fields_at_print As Boolean
Dim my_update_link_at_print As Boolean
Dim current_printer As String
' We are using a custom Document Property to save the serial number
' as we can edit this from the backstage properties advanced dialog
' if needed
ensure_cdp_exists cdp_name
Set my_form = New ufrmPrintNumberedCopies
With my_form
.txtStartNumber = Trim(ActiveDocument.Variables(cdp_name))
.txtCopies = default_copies
.Show
If Not .Result Then
Exit Sub
End If
End With
' We now switch to the document printer that has been customised to aleays
' print in duplex
current_printer = ActivePrinter
ActivePrinter = duplex_printer
' The update fields and links at print time are application properties so anly need setting once
' but we preserve the values so we can restore the current settings after we have finished printing
With Options
my_update_fields_at_print = .UpdateFieldsAtPrint
.UpdateFieldsAtPrint = True
my_update_link_at_print = .UpdateLinksAtPrint
.UpdateLinksAtPrint = True
End With
first_copy = CLng(Trim(my_form.txtStartNumber))
last_copy = first_copy + CLng(Trim(my_form.txtCopies)) - 1
' MsgBox "Now we do the printing", vbOKOnly 'comment this out once tested
' it is assumed that there is a field somewhere that references the customdocumentproperty
For copy_number = first_copy To last_copy ' uncomment the for loop once tested
ActiveDocument.Variables(cdp_name) = CStr(copy_number)
ActiveDocument.PrintOut copies:=1
Next
' save the next starting serial number
ActiveDocument.Variables(cdp_name) = last_copy + 1
' snipped as the remainder of the code is not changed
This is the code that was being used: 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 lCopyNumFrom 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" & Application.Name))
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 = 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
Screenshot 2018-05-29 20.19.15.png One sticking point I have when I modified the code from ufrmPrintNumberedCopies is that it doesn't recognise the hyphens for the page range... Code:
If Not IsNumeric(Trim(Me.txtPages)) Then
MsgBox "The pages required should be a number", vbOKOnly
Me.txtPages.SetFocus
Me.txtPages.SelLength = Len(Me.txtPages)
form_validates_ok = False
Exit Function
End If
|
|
#2
|
|||
|
|||
|
Well done. I'm impressed with your ability to extend the userform based on previous instructions.
Its very easy to update the code to validate the page range you want. The first trick is to use the Trim and Replace functions to get rid of any commas and hyphens and multiple spaces. You can then split the list of page numbers separated by spaces using the split function to give an array of page numbers You can then test each page number in turn for it being numeric. This is the updated code for the form_validates_ok function If it were for my personal use then there would be some further abstraction as the validation is quite repetitive. Code:
Private Function form_validates_ok() As Boolean
Dim txt_pages As String
Dim pages() As String
Dim a_page_number As Variant ' must be a variant for loop control
If Not IsNumeric(Trim(Me.txtCopies)) Then
MsgBox "The number of copies should be a number", vbOKOnly
Me.txtCopies.SetFocus
Me.txtCopies.SelLength = Len(Me.txtCopies)
form_validates_ok = False
Exit Function
End If
If Not IsNumeric(Trim(Me.txtStartNumber)) Then
MsgBox "The start number should be a number", vbOKOnly
Me.txtStartNumber.SetFocus
Me.txtStartNumber.SelLength = Len(Me.txtStartNumber)
form_validates_ok = False
Exit Function
End If
' convert the txtPages string into an array of page numbers in array pages
txt_pages = Trim(Me.txtPages)
txt_pages = replace(txt_pages, ",", " ")
txt_pages = replace(txt_pages, "-", " ")
Do While InStr(txt_pages, " ") > 0 ' checking for the presence of two spaves adjacent to each other
txt_pages = replace(txt_pages, " ", " ")
Loop
pages = split(txt_pages, " ") ' Note the absence of () when assigning to an array
For Each a_page_number In pages
If Not IsNumeric(a_page_number) Then
MsgBox "The pages required should be a number", vbOKOnly
Me.txtPages.SetFocus
Me.txtPages.SelLength = Len(Me.txtPages)
form_validates_ok = False
Exit Function
End If
Next
form_validates_ok = True
End Function
Code:
ActiveDocument.PrintOut copies:=1, pages:=my_form.txtPages |
|
#3
|
|||
|
|||
|
WOW, that was fast! Thank you!
I've got the forms code in no problems but when I amended the printout statement and run it, I get a Run-time error '13': Type mismatch dialog. I've highlighted the line that 'debug' doesn't seem to like. This is the module code Code:
Sub PrintNumberedCopiesSelect()
'
' PrintNumberedCopiesSelectionofPages macro
' Shortcut keys Alt+S
'
Const cdp_name As String = "CopyNum"
Const default_copies As String = "1"
Const duplex_printer As String = "hp deskjet 5550 series (HPA) duplex" ' change this to the name of your duplex printer
Dim my_form As ufrmPrintNumberedCopiesSelect
Dim copy_number As Long
Dim first_copy As Long
Dim last_copy As Long
Dim my_update_fields_at_print As Boolean
Dim my_update_link_at_print As Boolean
Dim current_printer As String
' We are using a custom Document Property to save the serial number
' as we can edit this from the backstage properties advanced dialog
' if needed
ensure_cdp_exists cdp_name
Set my_form = New ufrmPrintNumberedCopiesSelect
With my_form
.txtStartNumber = Trim(ActiveDocument.Variables(cdp_name))
.txtCopies = default_copies
.Show
If Not .Result Then
Exit Sub
End If
End With
' We now switch to the document printer that has been customised to aleays
' print in duplex
current_printer = ActivePrinter
ActivePrinter = duplex_printer
' The update fields and links at print time are application properties so anly need setting once
' but we preserve the values so we can restore the current settings after we have finished printing
With Options
my_update_fields_at_print = .UpdateFieldsAtPrint
.UpdateFieldsAtPrint = True
my_update_link_at_print = .UpdateLinksAtPrint
.UpdateLinksAtPrint = True
End With
first_copy = CLng(Trim(my_form.txtStartNumber))
last_copy = first_copy + CLng(Trim(my_form.txtCopies)) - 1
' MsgBox "Now we do the printing", vbOKOnly 'comment this out once tested
' it is assumed that there is a field somewhere that references the customdocumentproperty
For copy_number = first_copy To last_copy ' uncomment the for loop once tested
ActiveDocument.Variables(cdp_name) = CStr(copy_number)
ActiveDocument.PrintOut copies:=1, pages:=my_form.txtPages
Next
' save the next starting serial number
ActiveDocument.Variables(cdp_name) = last_copy + 1
' restore saved settings
With Options
.UpdateFieldsAtPrint = my_update_fields_at_print
.UpdateLinksAtPrint = my_update_link_at_print
End With
ActivePrinter = current_printer
End Sub
Sub ensure_cdp_exists(cdp_name As String)
'cdp is short for CustomDocumentProperty
' Searches for the a custom document property of cdp_name
' If not found the custom document property is created and assigned a value of 1
Const default_start_number As String = "1"
Dim my_cdp As DocumentProperty
For Each my_cdp In ActiveDocument.CustomDocumentProperties
If my_cdp.Name = cdp_name Then
Exit Sub
End If
Next
' We only get to this point if we don't find the target custom document property
' using the previous loop
ActiveDocument.CustomDocumentProperties.Add _
Name:=cdp_name, _
LinkToContent:=False, _
Value:=default_start_number, _
Type:=msoPropertyTypeString
' Be gentle with the user and let them know what has happened.
MsgBox _
Title:="Missing CustomDocumentProperty", _
prompt:="CustomDocumentProperty '" & cdp_name & "' was added to the document" & vbCrLf & vbCrLf & "A value of '" & default_start_number & "' was assigned'", _
Buttons:=vbOKOnly
End Sub
|
|
#4
|
|||
|
|||
|
Apologies. I don't check the actual printout statement as I don't want to waste reams of paper.
The help page for printout is here https://msdn.microsoft.com/en-us/VBA...ut-method-word I suspect the problem is that the pages parameter should be a variant but we are passing a string. Try changing to pages:=Cvar(my_form.txtPages) |
|
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Help with Case and Select case | brent chadwick | Word VBA | 34 | 10-18-2015 02:13 PM |
| Replace & case | Jennifer Murphy | Word | 1 | 02-11-2013 03:26 AM |
A macro that can insert FILENAME, sendkeys CTRL ALT T, paste clipboard, and nextline
|
kyjac85 | Word VBA | 13 | 09-20-2012 05:00 PM |
| Problem with the sendkeys in Win7 | vidyapakki | Excel Programming | 1 | 05-07-2012 11:10 PM |
From all UPPER CASE to Proper Case
|
davers | Word | 1 | 04-30-2009 12:41 PM |