亲爱的所有人,在下面的代码中将新单词模板应用于现有的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文档
发布评论