【发布时间】:2018-03-28 23:13:42
【问题描述】:
我在这里找到了开始使用脚本的非常有用的信息:
但我不能对多个 Word 文档执行相同的操作 - 例如同一文件夹中的 3 或 4 个 word 文档。
我尝试了命令ForEach,但总是收到错误消息。
有人可以帮我修改以下脚本以考虑路径文件夹中的所有word文档吗?
$path = "C:\fso\Test.docx"
$application = New-Object -ComObject word.application
$application.Visible = $false
$document = $application.documents.open($path)
$binding = "System.Reflection.BindingFlags" -as [type]
$customProperties = $document.CustomDocumentProperties
$typeCustomProperties = $customProperties.GetType()
$CustomProperty = "Client"
$Value = "My_WayCool_Client"
[array]$arrayArgs = $CustomProperty,$false, 4, $Value
Try {
$typeCustomProperties.InvokeMember(`
"add", $binding::InvokeMethod,$null,$customProperties,$arrayArgs) |
out-null
} Catch [system.exception] {
$propertyObject = $typeCustomProperties.InvokeMember(`
"Item", $binding::GetProperty, $null, $customProperties, $CustomProperty)
$typeCustomProperties.InvokeMember(`
"Delete", $binding::InvokeMethod, $null, $propertyObject, $null)
$typeCustomProperties.InvokeMember(`
"add", $binding::InvokeMethod, $null, $customProperties, $arrayArgs) |
Out-Null
}
$document.Saved = $false
$document.save()
$application.quit()
$application = $null
[gc]::collect()
[gc]::WaitForPendingFinalizers()
我也试过了
Get-ChildItem -path $path | Where-Object { $_.Name -like '*.docx' }
和ForEach cmdlet。
【问题讨论】:
-
您尝试将
ForEach-Object循环放在哪里以及错误消息是什么。我会把它放在$application.Visible = $false这一行之后,并在$application.quit()之前关闭它很可能
标签: powershell ms-word