PDA

View Full Version : Print on Letterhead every X pages



Joneze
07-03-2017, 09:06 AM
Hi,

I hope someone can point me in the right direction for a macro I am trying to create. So there are documents varying it legnth. For example there might be a 48 page document that is a 4 page letter that need to go out to different people.
(Note this is not a mail merge from word, this has been gernerated by a 3rd party application)



So what i would like my macro to do, is ask the user how many pages the letter is. They enter 4 into the prompt. The macro then prints the document but at the start of each new letter (every 4 pages) the first page is sent to the letterhead tray of the printer.



Sounds simple enough but everything i can find online is based on word doing the mailmerge in order to know each record. I would like the user to enter the number of pages and then use this in my macro.


Thanks in advance.

gmayor
07-03-2017, 09:46 PM
Not as simple as you might imagine but the following should do the job. You will have to identify the two print tray id numbers (here 7 and 267 which are appropriate to my Canon inkjet). The list at the top of macro gives the numbers for the standard tray allocations. If the numbers do not work for you then see http://www.gmayor.com/fax_from_word.htm which demonstrates a method of getting the tray ID numbers


Option Explicit

'wdPrinterAutomaticSheetFeed 7
'wdPrinterDefaultBin 0
'wdPrinterEnvelopeFeed 5
'wdPrinterFormSource 15
'wdPrinterLargeCapacityBin 11
'wdPrinterLargeFormatBin 10
'wdPrinterLowerBin 2
'wdPrinterManualEnvelopeFeed 6
'wdPrinterManualFeed 4
'wdPrinterMiddleBin 3
'wdPrinterOnlyBin 1
'wdPrinterPaperCassette 14
'wdPrinterSmallFormatBin 9
'wdPrinterTractorFeed 8
'wdPrinterUpperBin 1


Sub SplitMergeLetterToPrinter()
'Graham Mayor - http://www.gmayor.com - Last updated - 04 Jul 2017
Dim iLetters As Integer
Dim iCounter As Integer: iCounter = 1
Dim iPages As Integer
Dim strPages As String
Dim oRng As Range

Set oRng = ActiveDocument.Range
oRng.Collapse 0
iLetters = oRng.Information(wdActiveEndPageNumber)
strPages = InputBox("Enter the number of pages per letter")
If IsNumeric(strPages) Then
iPages = CInt(strPages)
If Not iLetters Mod iPages = 0 Then
MsgBox "There are " & iLetters & " pages in the document." & vbCr & _
"This does not divide equally into letters of " & iPages & " pages"
GoTo lbl_Exit
End If
Else
MsgBox "The value entered must be numeric!"
GoTo lbl_Exit
End If
While iCounter <= iLetters
PrintToTray 7, iCounter, iCounter
PrintToTray 267, iCounter + 1, iCounter + (iPages - 1)
iCounter = iCounter + iPages
Wend
lbl_Exit:
Exit Sub
End Sub

Private Sub PrintToTray(ByRef lngTray As Long, iStartPage As Integer, iEndPage As Integer)
'Graham Mayor - http://www.gmayor.com - Last updated - 04 Jul 2017
Dim lngDefTray As Long
lngDefTray = Options.DefaultTrayID
With Dialogs(wdDialogFilePrintSetup)
Options.DefaultTrayID = lngTray
ActiveDocument.PrintOut Background:=False, _
Range:=wdPrintFromTo, _
From:=Format(iStartPage), to:=Format(iEndPage)
Options.DefaultTrayID = lngDefTray
End With
lbl_Exit:
Exit Sub
End Sub

Joneze
07-04-2017, 01:37 AM
Much appriciated and it is almost exactly what I need. The issue is that it prints the first page of the whole document on letterhead and the rest on plain. I do have the correct tray numbers as i recorded the macro manually to check the numbers (257 and 258)

I also noticed that if I print a document manually selecting a page range to print of 3-4 and with page setup set for the first page to print on letterhead it still prints plain. It is as if word is only treating the very first page in the document as the first page and not the first page of the specified print range.

Thanks

Joneze
07-04-2017, 01:45 AM
Ok so it looks as though I also need to enter a section break based on the letter page count input from the user. Could you tell me how I can ammed the code to also insert a section break based on the users inputted value.


Many thanks again.

gmayor
07-04-2017, 04:08 AM
The macro creates separate print jobs for the first pages and the subsequent pages for each block of pages selected from the inputbox, and does not require section breaks. The first page of each block goes to one paper source and the subsequent pages go to the other paper source. You can confirm this with a couple of temporary message boxes e.g.


While iCounter <= iLetters
MsgBox "Printing page " & iCounter 'for testing
'PrintToTray 7, iCounter, iCounter
MsgBox "Printing pages " & iCounter + 1 & " to " & iCounter + (iPages - 1) 'for testing
'PrintToTray 267, iCounter + 1, iCounter + (iPages - 1)
iCounter = iCounter + iPages
Wend

I have tested the macros (obviously with my printer and Word 2016) and it works as you intend.

The macro lists the common tray allocation numbers at the top. If those correspond to your printer, then use the numbers associated with those trays as shown alongside. For example my printer has a sheet feed (associated with the number 7 ) which I have used in the macro. I used 267 for the main cassette, as I have used this before, but it should work with wdPrinterPaperCassette i.e. 14

My guess is that you probably need wdPrinterLowerBin or 2 and wdPrinterUpperBin or 1 for the two bin allocations

Joneze
07-04-2017, 08:12 AM
Thanks Gmayor. After some more testing it seems if I create a new word document with 12 pages the macro works as expected. However using a document generated from the 3rd party application the macro seems to have a mind of its own. My test document is 48 pages long for testing purposes I said my letter is 24 pages long. First page prints from wrong tray 25 prints on correct tray. Now I also noticed that the exported file from the 3rd party application exports as a .RTF document so thought this may be affecting the macro, but even after saving the document as word it gives the same results.

Think I may have to find another way around this.

gmayor
07-04-2017, 09:24 PM
If you are able to send a sample document to my e-mail address I will investigate further

Send it to supportATgmayor.com and put your forum username in the subject or it will be discarded.

gmayor
07-05-2017, 04:56 AM
For the benefit of anyone else with this issue, the problem related to section breaks in the document. The sections were each formatted to print the first and subsequent pages from different bins, which confuses the macro. It is easily addressed by adding


With ActiveDocument.PageSetup
.FirstPageTray = 0
.OtherPagesTray = 0
End With

to the start of the macro