为 Powershell MS-Word MailMerge 设置目标

Setting Destination for Powershell MS-Word MailMerge

我一直在尝试解决这个'simple'问题,但找不到问题的解决方案。

我正在尝试生成一个 powershell 脚本以 运行 MS-WORD 邮件合并并将文档(最好是 pdf)导出到特定文件位置。邮件合并 运行 成功,但它没有生成新文档,而是打印到默认打印机。我希望它生成一个新文档,这样我就可以将它保存到提供的目的地。

$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()

我猜我在邮件合并目标上做错了什么,但我在兜圈子。

提前感谢您提供的任何帮助。 (第一次发帖,请文静)

[edit] 总有一些你很重要的东西你忘记了。逐步执行此操作时,它会在到达保存文档的行之前生成邮件合并打印。当保存启动时,它是一个重命名原始邮件合并的单页文档。

替换此行:

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

通过这一行:

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

这一行

$doc.WdMailMerge.Destination.wdSendToNewDocument

应该更像这样:

$doc.MailMerge.Destination = 0

或者要使用 Word 的内置常量,您应该可以这样做(根据@Theo 的评论]:

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