我正在开发一个项目,我需要将几千个文档从HTML转换为Word。我使用此功能工作:如何在Powershell中强制执行文件操作?
function saveas-document ($docs) {
$opendoc = $word.documents.open($docs);
$savepath = $docs -replace [regex]::escape($htmPath),"$docpath"
$opendoc.saveas([ref]"$savepath", [ref]$saveFormat);
$opendoc.close();
}
近日,在测试时,我的工作停滞不前,因为文件之一是“另一用户”锁定。我收到一个对话框询问我是否需要阅读ony副本,如果稍后要编辑和合并更改,或者等待文档再次可用的通知。另外,在我选择打开文件的任何情况下,似乎都会覆盖我的$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
没有工作... :(见我的编辑以上.... – dwwilson66
对不起,对我的作品在生产 – JPBlanc
废话OK,我会继续挖掘。。不管怎么说,还是要谢谢你。 – dwwilson66