forked from TAo4ma/PS_Script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SetPropertyTest1.ps1
87 lines (75 loc) · 3.56 KB
/
SetPropertyTest1.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
function Set-DocumentProperty {
param (
[string]$Path, # ドキュメントのパス
[string]$PropertyName, # 設定するプロパティ名
[string]$PropertyValue # 設定するプロパティ値
)
try {
$word = New-Object -ComObject Word.Application
$doc = $word.Documents.Open($Path)
$binding = "System.Reflection.BindingFlags" -as [type]
[ref]$SaveOption = "Microsoft.Office.Interop.Word.WdSaveOptions" -as [type]
$propertySet = $false
# ビルトインプロパティを設定
try {
$Properties = $doc.BuiltInDocumentProperties
$pn = [System.__ComObject].InvokeMember("Item", $binding::GetProperty, $null, $Properties, $PropertyName)
[System.__ComObject].InvokeMember("Value", $binding::SetProperty, $null, $pn, $PropertyValue)
$propertySet = $true
} catch [System.Exception] {
Write-Host -ForegroundColor Blue "Builtin property '$PropertyName' not found or cannot be set."
}
# カスタムプロパティを設定
if (-not $propertySet) {
try {
$Properties = $doc.CustomDocumentProperties
$pn = [System.__ComObject].InvokeMember("Item", $binding::GetProperty, $null, $Properties, $PropertyName)
[System.__ComObject].InvokeMember("Value", $binding::SetProperty, $null, $pn, $PropertyValue)
$propertySet = $true
} catch [System.Exception] {
Write-Host -ForegroundColor Blue "Custom property '$PropertyName' not found or cannot be set."
}
# カスタムプロパティが存在しない場合、新規作成
if (-not $propertySet) {
try {
$Properties.Add($PropertyName, $false, 4, $PropertyValue) # 4 corresponds to msoPropertyTypeString
Write-Host -ForegroundColor Green "Custom property '$PropertyName' created and set to '$PropertyValue'."
$propertySet = $true
} catch [System.Exception] {
Write-Host -ForegroundColor Red "Failed to create and set custom property '$PropertyName'."
}
}
}
# ドキュメントを保存して閉じる
$doc.Save()
$doc.Close([ref]$SaveOption::wdSaveChanges)
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($Properties) | Out-Null
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($doc) | Out-Null
Remove-Variable -Name doc, Properties
$word.Quit()
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($word) | Out-Null
Remove-Variable -Name word
[gc]::collect()
[gc]::WaitForPendingFinalizers()
if ($propertySet) {
Write-Host "Property '$PropertyName' set to '$PropertyValue'." -ForegroundColor Green
return $true
} else {
Write-Host "Failed to set property '$PropertyName'." -ForegroundColor Red
return $false
}
} catch {
Write-Host "An error occurred: $_" -ForegroundColor Red
return $false
}
}
# 関数の呼び出し例
$path = "C:\Users\y0927\Documents\GitHub\PS_Script\技100-999.docx"
$propertyName = "batter"
$propertyValue = "近藤さんq"
$result = Set-DocumentProperty -Path $path -PropertyName $propertyName -PropertyValue $propertyValue
if ($result) {
Write-Host "Property set successfully." -ForegroundColor Green
} else {
Write-Host "Failed to set property." -ForegroundColor Red
}