我正在做一个项目,我需要将几千个文档从 HTML 转换为 Word。我正在使用这个功能:
function saveas-document ($docs) {
$opendoc = $word.documents.open($docs);
$savepath = $docs -replace [regex]::escape($htmPath),"$docpath"
$opendoc.saveas([ref]"$savepath", [ref]$saveFormat);
$opendoc.close();
}
最近,在测试时,我的工作因其中一个文件被“另一个用户”锁定而停滞不前。我收到一个对话框,询问我是否想要只读副本,是否想要稍后编辑和合并更改,或者等待文档再次可用的通知。此外,在我选择打开文件的任何一种情况下,这似乎都会覆盖我的$word.Visible-$False
参数。
我未能成功识别锁定我的文档的“另一个用户”。所有文档都在本地非共享驱动器上,所以我不确定是什么让它们被锁定。根据文件所在的位置,我确信不需要编辑任何内容,我可以强制打开文件,然后Save As...
我可以-force
在这个函数中用作参数吗?放置该参数的最佳位置在哪里?
编辑:根据下面的 JPBlanc,我尝试停止 Word,每条记录出现三个错误。
Method invocation failed because [Microsoft.Office.Interop.Word.DocumentClass] doesn't contain a method named 'quit'.
At C:\users\x46332\desktop\sad003.ps1:69 char:18
+ $opendoc.quit <<<< ()
+ CategoryInfo : InvalidOperation: (quit:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Exception calling "SaveAs" with "16" argument(s): "COM object that has been separated from its underlying RCW cannot be
used."
At C:\users\x46332\desktop\sad003.ps1:73 char:20
+ $opendoc.saveas <<<< ([ref]"$savepath", [ref]$saveFormat);
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Exception calling "Close" with "3" argument(s): "COM object that has been separated from its underlying RCW cannot be u
sed."
At C:\users\x46332\desktop\sad003.ps1:74 char:19
+ $opendoc.close <<<< ();
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException