PowerShell Word邮件合并脚本

时间:2014-08-18 15:25:11

标签: powershell ms-word mailmerge

我有一个脚本曾经在旧版本的Office上正常工作,但在2010年现在无法正确邮件合并文档。

有趣的是,如果我加载邮件合并文档(通过双击),它会告诉我它的邮件合并文档,并需要确认其源数据文件。哪个都没问题但是这个行为不一样,如果我通过PS打开它只是作为普通文档加载,代码的麻烦片段如下:

if ($choice) {
    $word = New-Object -com "Word.Application"
    $template=$word.Documents.open("$template_path\$choice",$word.wdReadOnly)
    $word.ActiveDocument.Mailmerge.Execute()
    $template.close([ref]$word.wdDoNotSaveChanges)  
    $word.Visible = 'True'
}

我确实遇到过这个问题的链接,但我不是专家编码员,而且信息在我脑海中浮现了一点,他说:

Sub OpenWord(fileName)
    Set Word = WScript.CreateObject("Word.Application")
    Word.Visible = True
    Set doc = Word.Documents.Open(fileName)
End Sub


I have seen some issues with such code if the word document has a mail merge data source associated with it and you try to execute it (see Word Mail Merge).
doc.MailMerge.Execute True

Trying to execute the mail merge generates a ‘This method or property is not available because the document is not a mail merge main document.’ But I know this is wrong because double clicking on the file reveals a data source associated with the document. Another way to open a word document and circumvent this issue is to use the run command such as:

Sub OpenWord(fileName)
    Set WshShell = WSCript.CreateObject("WScript.Shell")
    WshShell.Run fileName, 8, False
End Sub

有人可以解释我如何获取此信息并将其应用到我的PowerShell脚本中吗?

1 个答案:

答案 0 :(得分:0)

确保Windows注册表设置" SQLSecurityCheck" support.microsoft.com/kb/825765中已描述为Word 2010以及旧版本的Word设置。