Microsoft Word Print Macros

Posted on


These are some simple Microsoft Word macros which print the current document to a particular tray of the chosen printer. The ‘printletters’ macro below is quite possibly the most useful script I have ever written. When my colleagues found out about this baby, they all demanded I set it up on their computers, and I became the hero of the workplace.

Well, that’s what I’d like to think, anyway.

I wrote these little macros to help diminish the annoyance of having to select the correct printer tray each time I wanted to print a document on a different kind of paper at work. When we send out letters to clients, we have to print the first page of their letter on letterhead and the remaining pages on plain white paper. We also have to print a file copy of the letter entirely on yellow paper. This involves: - Checking which tray the printer driver is currently set to print to, and changing it to letterhead, if it’s not already selected; - Printing just the first page of the document (on letterhead); - Changing the selected print tray to white; - Printing the remainder of the document (on white) - Changing the selected print tray to yellow; - Printing the entire document again.

Which is a pretty annoying process, particularly if you’re writing lots of letters longer than one page in length.

Enter the macros. The first one deals with this situation, printing the whole document on yellow paper, then just the first page on letterhead, then the rest of the pages (if there are any more) on white paper. With just *one* click of a button, a single keystroke combination, or a wave of your magic wand, it is done.

Here it is: Sub printletters() ' ' printletters Macro ' '

Dim sCurrentPrinter As String Dim sTray As Long Dim TtlPgs As Integer TtlPgs = Selection.Information(wdNumberOfPagesInDocument) sCurrentPrinter = ActivePrinter sTray = Options.DefaultTrayID ActivePrinter = "PRINTER NAME"

With Options .DefaultTrayID = "258" 'yellow End With Application.PrintOut FileName:="" 'print all

With Options .DefaultTrayID = "259" 'letterhead End With ActiveDocument.PrintOut Range:=wdPrintRangeOfPages, Pages:="1" 'print just first page

If TtlPgs > 1 Then With Options .DefaultTrayID = "257" 'white End With ActiveDocument.PrintOut Range:=wdPrintRangeOfPages, Pages:="2-" & TtlPgs 'print remainder of pages End If

With Options .DefaultTrayID = sTray End With ActivePrinter = sCurrentPrinter

End Sub

Now if you want to use this macro, you’ll need to enter the name of the printer where it says “PRINTER NAME”. I’m sorry, I can’t tell you what it’s called, so you’ll need to figure that one out on your own. Also – you’ll need to figure out what the default tray ID’s are too. Unfortunately I can’t remember how I figured out what mine are, I think I actually just guessed. In this case they are: Tray 1 (white): 257 Tray 2 (yellow): 258 Tray 3 (letterhead): 259

If you have no idea what your tray ID’s are, I know there is a way of figuring it out, but I just don’t know what it is. I suggest you try Googling, or guessing like I did.

This next macro is for printing the entire current document to white paper only. I use this one when I need to print to white only, and can’t be bothered checking and changing which tray the printer is currently set to use. Sub printwhite() ' ' print to white paper only ' '

Dim sCurrentPrinter As String Dim sTray As Long 'Dim TtlPgs As Integer 'TtlPgs = Selection.Information(wdNumberOfPagesInDocument) sCurrentPrinter = ActivePrinter sTray = Options.DefaultTrayID ActivePrinter = "PRINTER NAME"

With Options .DefaultTrayID = "257" 'white End With Application.PrintOut FileName:="" 'print all

With Options .DefaultTrayID = sTray End With ActivePrinter = sCurrentPrinter

End Sub

And this last one is used for letters I don’t need a file copy of. It prints just the first page on letterhead, and the rest on white. Sub printletterhead() ' ' prints first page to to letterhead, the rest on white ' '

Dim sCurrentPrinter As String Dim sTray As Long Dim TtlPgs As Integer TtlPgs = Selection.Information(wdNumberOfPagesInDocument) sCurrentPrinter = ActivePrinter sTray = Options.DefaultTrayID ActivePrinter = "PRINTER NAME"

With Options .DefaultTrayID = "259" 'letterhead End With ActiveDocument.PrintOut Range:=wdPrintRangeOfPages, Pages:="1" 'print just first page

If TtlPgs > 1 Then With Options .DefaultTrayID = "257" 'white End With ActiveDocument.PrintOut Range:=wdPrintRangeOfPages, Pages:="2-" & TtlPgs 'print remainder of pages End If

With Options .DefaultTrayID = sTray End With ActivePrinter = sCurrentPrinter

End Sub


 

Share this!


facebooktwittergoogle_pluslinkedin


 

7 Responses to “Microsoft Word Print Macros”

  1. Alain Martin says:

    Just thought I would let you know that we use a tool from ThinPrint that can interogate the vendor printer driver and extract the tray id’s. The tool is called PrinterTrayInfo and you can download it from here http://www.it-con.com/english/thinprint/thinprint.html

    Hope this helps

    Alain

    • mikey says:

      Dear Alain

      That ThinPrint tool is awesome, works perfectly for me.

      Funnily enough, just yesterday I had to figure out the tray names on another printer, and that would really have come in handy! I ended up having to write some more VBA and use a listprintbox to get the tray numbers, but ThinPrint works much better.

      Oh well, good to know for future reference. Thanks Alain!

      Mikey

  2. craig says:

    Great code. I needed this for a solicitors site so glad I found it.

    They are using MS word 2010 and i added as a macro, onl thing is it seems to only work when i pre-configure the printer trays in “page setup” is this a usual problem?

    Thanks

    • mikey says:

      Hi Craig

      Thank you for your comment!

      Yes, this is occasionally a problem for me as well. When printing certain documents, this will simply not do what you want it to, and will always print the first page from tray one and the rest from tray two. Unfortunately I have no idea why it does this, and the only solution I can come up with is to create a new document, copy & paste the content from the original document, and run the macro on the new document.

      Anyway, have you set the tray ID’s correctly in the macro?

      I know I’ve adjusted my own version of this code since posting this. I can’t remember what changes I made, so I’ll take a look and see if it helps at all.

      Kind regards

      Mikey

  3. craig says:

    So i asked a perfectly normal question and you delete my comment?

    • mikey says:

      Hi Craig, sorry about that! I didn’t delete your comment, I just hadn’t had time to moderate and respond yet as I’ve been very busy.

      Please see my reply below above

      Mikey

  4. Tim Kempster says:

    Hi Mikey We have a tool that we sell online which solves all these problems and more and wraps it up in a simple interface that users might consider.

    You can find out more at http://www.trayselector.com Tim

Leave a Reply