简体   繁体   中英

MS Word page layout adjustment with powershell

Any idea how to change the print page layout in MS Word with PowerShell? Let's say for example I want to setup the document to print with Letterhead and plain paper or simply switch the duplex printing (one side or both sides) (pardon the french display ;-)) : 在此处输入图片说明

I know how to create the document and all but not how to adjust these printing preferences.

Basically I need to know how to set these properties, the final script would be something like :

$Word = New-Object -ComObject Word.Application
$Document = $Word.Documents.Add()
$Selection = $Word.Selection
$Selection.TypeText("Testing")

#SET PRINT LAYOUT OPTIONS HERE, WHICH I DON'T KNOW HOW :)

$File = 'C:\temp\test.docx'
$Document.SaveAs([ref]$File,[ref]$SaveFormat::wdFormatDocument)
$word.Quit()

I can't test it yet, but had the same question and hopefully now the answer:

With $PrinterSettings = new-object System.Drawing.Printing.PrinterSettings you can see most of the (default) printer settings. $PrinterSettings.CanDuplex to check if your printer supports Duplex.

$DefaultPrinter = Get-WmiObject -Query " SELECT * FROM Win32_Printer WHERE Default=$true" | Select -Expand Name
Set-PrintConfiguration $DefaultPrinter -DuplexingMode TwoSidedLongEdge -PaperSize Letterhead

That should do the Job.
You can also do:

$PrinterSettings = Get-PrintConfiguration -PrinterName "Microsoft XPS Document Writer"
$PrinterSettings.DuplexingMode = "TwoSidedLongEdge"
$PrinterSettings.PaperSize = "Letterhead"

For more info about the options: Set-PrintConfiguration

DuplexingMode options:

OneSided
TwoSidedLongEdge
TwoSidedShortEdge

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM