使用PowerShell使用新文档模板更新现有Word文档

编程入门 行业动态 更新时间:2024-10-21 13:24:34
本文介绍了使用PowerShell使用新文档模板更新现有Word文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

亲爱的所有人,在下面的代码中将新单词模板应用于现有的Word文档,但我收到此错误:

Dear All, below code to apply new word template to existing word document but I am getting this error:

异常设置&"SaveAs":无法转换"C:\XXXXXXXXX \ test.docx""类型"psobject"的值输入"对象"。 在C:\ XXXXXXXX \wordtemplatechange.ps1:23 char:1 + $ wordopen2.SaveAs([ref] $ destination,[ref] $ SaveFormat) + ~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ $ b + CategoryInfo   ;: NotSpecified :( :) [],MethodException + FullyQualifiedErrorId:RuntimeException

Exception setting "SaveAs": Cannot convert the "C:\XXXXXXXXX\test.docx" value of type "psobject" to type "Object". At C:\XXXXXXXX\wordtemplatechange.ps1:23 char:1 + $wordopen2.SaveAs([ref]$destination,[ref]$SaveFormat) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodException + FullyQualifiedErrorId : RuntimeException

CODE Refrence from: blog.konffaaja/2017/02/22/update-word-documents-to-a -new-document-template-by-using-powershell /

CODE Refrence from:blog.konffaaja/2017/02/22/update-word-documents-to-a-new-document-template-by-using-powershell/

代码:

$ word = New-Object -ComObject Word.Application $ word2 = New-Object -ComObject Word.Application #[ref] $ SaveFormat =" microsoft.office.interop.word.WdSaveFormat" -as [type] $ SaveFormat = [Microsoft.Office.Interop.Word.WdSaveFormat] :: wdFormatXMLDocument $ word.Visible = $ false $ word2.Visible = $ false $ path =" C:\ xxxxxx \ test" $ $ pathnew =" C:\ xxxxxxx \ testnew" Get-ChildItem -Path $ path -Include"* .docx"-Recurse | ForEach-Object { $ wordopen = $ word.Documents.Open($ _。FullName) $ newdoc = Join-Path -Path $ pathnew -childPath" Template .dotx" $ $ destination = Join-Path -Path $ pathnew -childPath $ _。名称 #从模板创建新的docx $ wordopen2 = $ word2.Documents.Add($ newdoc) #copy来自已打开docx的数据 $ range = $ wordopen.Range() $ copy = $ range.Copy() #将数据粘贴到新创建的docx中$ $ range2 = $ wordopen2.Range() $ range2.Paste($ copy) #Save #$ wordopen2.saveas([ref] $ destination,[ref] $ saveFormat :: wdFormatDocument) $ wordopen2.SaveAs([ref] $ destination,[ref] $ SaveFormat) ---在此行中获取错误。 $ wordopen2.Close() $ wordopen.Close() }

$word=New-Object -ComObject Word.Application $word2=New-Object -ComObject Word.Application #[ref]$SaveFormat = "microsoft.office.interop.word.WdSaveFormat" -as [type] $SaveFormat=[Microsoft.Office.Interop.Word.WdSaveFormat]::wdFormatXMLDocument $word.Visible=$false $word2.Visible=$false $path="C:\xxxxxxx\test" $pathnew="C:\xxxxxxx\testnew" Get-ChildItem -Path $path -Include "*.docx" -Recurse | ForEach-Object { $wordopen=$word.Documents.Open($_.FullName) $newdoc = Join-Path -Path $pathnew -childPath "Template.dotx" $destination = Join-Path -Path $pathnew -childPath $_.Name # Create new docx from template $wordopen2=$word2.Documents.Add($newdoc) #copy data from opened docx $range = $wordopen.Range() $copy = $range.Copy() # paste data to new created docx $range2 = $wordopen2.Range() $range2.Paste($copy) # Save #$wordopen2.saveas([ref]$destination, [ref]$saveFormat::wdFormatDocument) $wordopen2.SaveAs([ref]$destination,[ref]$SaveFormat) --- Getting Error in this line. $wordopen2.Close() $wordopen.Close() }

请帮助

推荐答案

如果你愿意的话格式正确并缩进你的代码,你会立即看到错误。

If you would properly format and indent your code you would see the error immediately.

更多推荐

使用PowerShell使用新文档模板更新现有Word文档

本文发布于:2023-11-28 10:07:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641968.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文档   模板   PowerShell   Word

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!