PowerShell Word邮件合并脚本

PowerShell Word邮件合并脚本,powershell,ms-word,mailmerge,Powershell,Ms Word,Mailmerge,我有一个脚本,以前在旧版本的Office上运行得很好,但在2010年,它现在无法正确地邮寄合并文档 有趣的是,如果我加载邮件合并文档(通过双击),它会告诉我它是邮件合并文档,并且需要确认它的源数据文件。这一切都很好,但是如果我通过PS打开它只是作为普通文档加载,那么这种行为就不一样了,下面是一段麻烦的代码片段: if ($choice) { $word = New-Object -com "Word.Application" $template=$word.Documents.o

我有一个脚本,以前在旧版本的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脚本中吗?

请确保windows注册表设置“SQLSecurityCheck”在SupFut.MySt.ToM/KB/825765中已经描述了Word 2010以及Word的旧版本。

我可能认为Windows中的注册表设置已经被修改为Word的早期版本,但不是为2010版本。这是我们最初遇到的问题,但遗憾的是产生了一个与我现在遇到的不同的错误,即“此方法或属性不可用,因为该文档不是邮件合并主文档。”我只能说,我尝试了一个版本的PowerShell代码(没有if{}阻止并替换合适的.docx.的全名,并将SQLSecurityCheck设置为1(默认值)我收到与您完全相同的消息。将其设置为0后,合并功能将正常运行。在我的系统上,密钥位于HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Word\Options。再次感谢Bibadia,它确实有效。还有两个因素导致了问题:一个是合并文件位于只读的C根目录上,另一个是代码$template=$Word.Documents.open(“$template\u path\$choice”、$word.wdReadOnly)需要是$template=$word.Documents.add($template\u path\$choice”)并且它现在可以工作:)谢谢,请添加作为分数的答案!好的,很高兴它现在起作用了。