簡體   English   中英

為 Powershell MS-Word MailMerge 設置目標

[英]Setting Destination for Powershell MS-Word MailMerge

我一直在嘗試解決這個“簡單”的問題,但找不到問題的解決方案。

我正在嘗試生成 powershell 腳本來運行 MS-WORD 郵件合並並將文檔(最好是 pdf)導出到特定文件位置。 mailmerge 運行成功,但它不會生成新文檔,而是打印到默認打印機。 我希望它生成一個新文檔,然后我可以將它保存到提供的目的地。

$DocumentName = 'MailMerge.docx'
$OutputFilename = 'Output.pdf'
$word=new-object -com Word.Application
$word.Visible = 'True'
$doc=$word.Documents.Open($DocumentName)
$doc.WdMailMerge.Destination.wdSendToNewDocument
$doc.Mailmerge.Execute()
$word.ActiveDocument.SaveAs([ref] $Outputfilename, [ref] 17)
$Doc.close()
$word.Quit()

我猜我在郵件合並目的地做錯了什么,但我在繞圈子。

提前感謝您提供的任何幫助。 (第一次發帖,請輕拍)

[編輯]總有一些重要的事情你會忘記。 逐步執行此操作時,它會在到達保存文檔的行之前生成郵件合並打印。 當保存開始時,它是一個重命名原始郵件合並的單頁文檔。

這條線

$doc.WdMailMerge.Destination.wdSendToNewDocument

應該更像這樣:

$doc.MailMerge.Destination = 0

或者要使用 Word 的內置常量,您應該能夠做到這一點(在 @Theo 發表評論之后):

$doc.MailMerge.Destination =  [Microsoft.Office.Interop.Word.WdMailMergeDestination]::WdSendToNewDocument

替換這一行:

$word.ActiveDocument.SaveAs([ref] $Outputfilename, [ref] 17)

通過這一行:

$doc.SaveAs([ref] $Outputfilename, [ref] 17)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM