PDA

View Full Version : Here's a challenge: Set Paper Size to custom printer form



florismk
10-17-2014, 07:02 AM
Here's a challenge for you all. I've looked everywhere, and it's been bugging me for years that I've never found an answer. If anyone has an answer, you earn my undying gratitude.

We have a print server, where a Custom Form has been defined for our letterhead paper. This Custom Form, named "Company Letterhead" is nothing but A4 paper size with a different name.
We created this Custom Form for one reason only: to link it to the correct (letterhead) paper trays in the server-side printer settings of various printers. The result is that we can select the "Company Letterhead" paper size in a document, and the document will print to letterhead paper on every printer (provided the tray choice is set to "Automatically select"). Since the paper size is stored with the document, this trick ensures that a document prints to letterhead regardless of the user's default printer, and even if he/she switches to a different printer. This way, no one but our support staff needs to remember in which tray the letterhead paper is on each printer.

The "Company Letterhead" Custom Form behaves exactly like a standard paper size, in that it is presented in the Paper Size dropdown in the Ribbon and in Page Setup.
And it is uniquely identified somehow, since it is retained with the document.

What I need is a way to:
A. Set any document to "Company Letterhead" paper from code.
B. Also through code, detect if a document has "Company Letterhead" selected.

The challenge is this: I can't find the paper size choice anywhere.

When I macro-record my actions in setting a document to "Company Letterhead" paper size, and do the same setting a document to A4, the resulting code is identical, setting every property of the PageSetup object (except, oddly enough, .PaperSize) to the same values in either case (code included below).

When I Debug.Print the PageSetup.PaperSize property for an A4 document, it reports wdPaperA4 (7).
When I Debug.Print the same for a "Company Letterhead" document, it reports the same value of 7.

So even though I know for a fact that the "Company Letterhead" paper size choice is stored with the document, I can't for the life of me find that choice in the object model.

Any ideas, anyone?


With Selection.PageSetup
.LineNumbering.Active = False
.Orientation = wdOrientPortrait
.TopMargin = CentimetersToPoints(2.54)
.BottomMargin = CentimetersToPoints(2.54)
.LeftMargin = CentimetersToPoints(2.54)
.RightMargin = CentimetersToPoints(2.54)
.Gutter = CentimetersToPoints(0)
.HeaderDistance = CentimetersToPoints(1.25)
.FooterDistance = CentimetersToPoints(1.25)
.PageWidth = CentimetersToPoints(21)
.PageHeight = CentimetersToPoints(29.7)
.FirstPageTray = wdPrinterDefaultBin
.OtherPagesTray = wdPrinterDefaultBin
.SectionStart = wdSectionNewPage
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.VerticalAlignment = wdAlignVerticalTop
.SuppressEndnotes = False
.MirrorMargins = False
.TwoPagesOnOne = False
.BookFoldPrinting = False
.BookFoldRevPrinting = False
.BookFoldPrintingSheets = 1
.GutterPos = wdGutterPosLeft
End With

gmaxey
10-17-2014, 11:54 AM
I hope that I am wrong, but I don't think you are going to find a VBA solution. The issue is that .PaperSize as seen by VBA is not a "named" value (like A4 or Company Letterhead). It is a constant with the value determined by the .height and .width set for the page. Since your Company Letter head is an exact match for A4 then 7 or wdPaperA4 is what is returned. If you tweaked you paper size just a smidge then it would return wdPageCustom or 41.

SamT
10-17-2014, 04:50 PM
wdDialogPageSetup & paper_size ?

florismk
10-18-2014, 01:17 AM
gmaxey, that's excellent! We can easily adjust the custom form to be half a mm narrower, and that at least would enable me to detect Company Letterhead documents. I'd have to experiment to see if I can also set documents to letterhead by setting the narrower width, but who knows...

SamT, are you suggesting that I might be able to read/write the Page Setup dialog directly? It's been my experience that calling up built-in dialogs doesn't enable me to do anything, since they display modally, so no code can run while they're displayed...

florismk
10-18-2014, 01:26 AM
SamT, unfortunately, the .PaperSize property in the PageSetup dialog reflects the .PaperSize property of the document, so comes with the same challenge.

SamT
10-18-2014, 04:27 AM
Greg is a Word MVP, and you have been at this for years. I am merely an Excel coder who can't resist a challenge. But my brief bit of research made me think that Word dialogs are a bit different from Excel's.

I was thinking more along the lines of a custom dialog class to retrieve or set the paper size. I have not pondered the mechanics of that. There are also hidden members of MSO Applications that one might be able to access with some API or another. :dunno

Finally, would it help to record the paper size in the Company Letterhead's or Normal.dot(x)'s Custom Properties ?

florismk
10-18-2014, 05:26 AM
SamT, thanks again for your help!

What you're suggesting: sticking the information in the Custom Properties, is not in itself a solution, given that the Company Letterhead form is a print server form, with no relation to any Word template. (Also, for unrelated reasons, I break the links between documents and their templates immediately after they've been created, by linking them to Normal, so I can't even tell from the attached template if it might be a letterhead document.)

However, your suggestion is exactly what I'm going to do on a more abstract level, based on gmaxey's idea: abuse/maim the custom form width property to be able to recognize it. This is the only way, since these custom forms have only Name, Width and Height properties on the print server. So I'll just go ahead and use those as custom properties, changing the width by so little that it won't make a difference to documents, while still being recognizable.

SamT
10-18-2014, 05:40 AM
:thumb