The Scene
What I am trying to do is programmatically convert an MS word (.docx) file to a PostScript file (.ps). I am doing this by creating a PostScript printer with one of the default PostScript printer drivers bundled with MS Windows and then printing the Word doc using this printer from Word. The catch is I am trying to do this with a custom page size, i.e. the height and width do not match any of the standard paper sizes ie. A4, A3, Letter etc
If I do this manually in MS Word everything works as expected BUT only if I set the Page Setup paper size to PostScript Custom Page Size. If its not set to this value the output page size is one of the pre-defined page sizes i.e. B5 (default).
But if I set the Paper size to PostScript Custom Page Size and then print with the same printer the output file is the correct height and width as set on the document, in this case, 181mm x 260mm
The Problem
I cant find a way to programmatically set the Page Setup Paper size value to the value "PostScript Custom Page Size", and if I dont set this value then the custom height and width are ignored.
What have I already tried
I have tried doing the following:
Using the Word COM objects in PowerShell
... #create com object $word = New-Object -com Word.Application #dont open word UI $word.visible = $false #open input file $doc = $word.Documents.Open($inputfile) $width = [double]$word.MillimetersToPoints($widthInMM) $height = [double]$word.MillimetersToPoints($heightInMM) #set page setup width and height $doc.PageSetup.PageWidth = $width $doc.PageSetup.PageHeight = $height #save the changes $doc.Save() $pBackGround = 0 $pAppend = 0 $pRange = 0 #print the file to default printer (i.e. ps printer) $doc.printout([ref]$pBackGround,[ref]$pAppend,[ref]$pRange,[ref]$outputfile) ...Looking at the MS docs, the PageSetup object has a PageSize property, which says the following on the page
Setting the PageHeight or PageWidth property changes the PaperSize property to wdPaperCustom.
And looking at the PaperSize property its an enum, WdPaperSize, which has the following values But as you can see by the quote above, if you set the height and width the paper size will be set to the wdPaperCustom value. BUT this is not the same as PostScript Custom Page Size, which from what I have read this is not one of the valid enum values.
- Pure PowerShell The only way to print a word (docx) file is using the Start-Process command with verb Print. If you dont want to use the default printer you can pipe it to the out-printer command
Start-Process $file -verb Print | out-printer -name "PrinterName"
This prints the document, but actually opens up Word to print which has 2 problems
a. You have to manually specify the output file name
b. It still uses the MS Word default page settings
Recording a VBA Macro: Recording setting the correct paper size doesn't record setting it to PostScript Custom Page Size. This is what the macro looks like
With Selection.PageSetup .LineNumbering.Active = False .Orientation = wdOrientPortrait .TopMargin = MillimetersToPoints(13) .BottomMargin = MillimetersToPoints(13) .LeftMargin = MillimetersToPoints(13) .RightMargin = MillimetersToPoints(13) .Gutter = MillimetersToPoints(3) .HeaderDistance = MillimetersToPoints(12.5) .FooterDistance = MillimetersToPoints(12.5) .PageWidth = MillimetersToPoints(181) .PageHeight = MillimetersToPoints(260) .FirstPageTray = wdPrinterDefaultBin .OtherPagesTray = wdPrinterDefaultBin .SectionStart = wdSectionNewPage .OddAndEvenPagesHeaderFooter = True .DifferentFirstPageHeaderFooter = True .VerticalAlignment = wdAlignVerticalTop .SuppressEndnotes = False .MirrorMargins = True .TwoPagesOnOne = False .BookFoldPrinting = False .BookFoldRevPrinting = False .BookFoldPrintingSheets = 1 .GutterPos = wdGutterPosLeft End WithAs you can see above there is no mention of the paper size being set to any value. I haven't tried this in c# or .NET because they all seem to use the COM Object API which reverts to my issues in 1.
I think the issue is that Word seems to ignore the printer settings, even Microsoft seems to admit this
With the printer I am creating a PostScript printer defining the specific paper size, height and width, but MS Word when printing ignores these settings and uses its own default settings. Even though the height and width of the pages in Word are set correctly its the paper size property that seems to be messing things up.
So the only logical thing I can think of is remove Word from the mix. The issue there is I cant find anything that handles Word properly. You can just send the file to the printer say in PowerShell, but it seems to still open Word and use the Word settings again.
Does anyone know of a way around this or a way to programmatically set the paper size to PostScript Custom Page Size



wd-enumerator available.wdPaperSize-enum has a numerical equivalent You might test setting the property with values "outside" the enum range (starting with 42) to see 1) Whether Word accepts the values and 2) if yes, which option in your listing the value corresponds to. Note that, even if this works, the value that corresponds on your machine may not be the same as on other machines... (Note that I'd test in VBA since that would be faster and more "immediate" than out-of-process code.)