我一直在尝试解决这个'简单'的问题,但找不到解决问题的方法。
我正在尝试生成一个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()
我想我在合并邮件的目标位置上做错了,但是我转了一圈。
谢谢您能提供的任何帮助。(初次发布,请保持温柔)
[编辑] 总有一些重要的事情你忘记了。逐步执行此操作时,它会在到达用于保存文档的行之前生成mailmerge打印。当保存开始时,它是一个重命名原始邮件合并的单页文档。
替换此行:
$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