gpt4 book ai didi

powershell - 如何更改多个 Word 文档的自定义属性

转载 作者:行者123 更新时间:2023-12-03 16:42:10 27 4
gpt4 key购买 nike

我在这里找到了非常有用的信息来开始使用脚本:

http://blogs.technet.com/b/heyscriptingguy/archive/2010/04/06/hey-scripting-guy-how-can-i-add-custom-properties-to-a-microsoft-word-document.aspx

但我不能对几个 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小命令。

最佳答案

正如马特建议的那样,我会在应用程序打开后放入一个 ForEach 循环。我还将在 ForEach 循环中添加关闭当前文档。就像是:

$path = "C:\fso\*.docx"
$application = New-Object -ComObject word.application
$application.Visible = $false
ForEach($File in (GCI $path|Select -Expand FullName)){
$document = $application.documents.open($file)
$binding = "System.Reflection.BindingFlags" -as [type]

<Other Commands>

$document.Saved = $false
$document.save()
$document.Close()
}
$application.quit()
$application = $null
[gc]::collect()
[gc]::WaitForPendingFinalizers()

关于powershell - 如何更改多个 Word 文档的自定义属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27781428/

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