gpt4 book ai didi

powershell - 如何在Powershell中强制执行文件操作?

转载 作者:行者123 更新时间:2023-12-03 08:08:08 25 4
gpt4 key购买 nike

我正在一个项目中,我需要将数千个文档从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

最佳答案

尝试将readonly变体添加到方法中,这是第二个变体,请参见http://msdn.microsoft.com/en-us/library/office/bb216319(v=office.12).aspx

即:

$opendoc = $word.documents.open($docs,$false,$true);

关于powershell - 如何在Powershell中强制执行文件操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14158962/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com