forked from TAo4ma/PS_Script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordproperties_getset.ps1
141 lines (123 loc) · 4.49 KB
/
wordproperties_getset.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# Wordアプリケーションを開始
$word = New-Object -ComObject Word.Application
$word.Visible = $true
# 指定されたWordファイルを開く
$doc = $word.Documents.Open("C:\Users\y0927\Documents\GitHub\PS_Script\技100-999.docx")
# ドキュメントプロパティを取得
$properties = $doc.BuiltInDocumentProperties
# プロパティの名前と値を表示
foreach ($property in $properties) {
try {
$name = $property.GetType().InvokeMember("Name", 'GetProperty', $null, $property, $null)
$value = $property.GetType().InvokeMember("Value", 'GetProperty', $null, $property, $null)
Write-Output "$($name): $value"
} catch {
Write-Output "Error accessing property: $_"
}
}
# 新しいセクションを追加
$range = $doc.Content
$range.Collapse(0) # 0 corresponds to wdCollapseEnd
$range.InsertBreak(2) # 2 corresponds to wdSectionBreakNextPage
$newSection = $doc.Sections.Item($doc.Sections.Count)
# プロパティの値を保存する配列を作成
$values = @()
# プロパティが存在するか確認し、存在しない場合はデフォルト値を設定
try {
$creationDate = $properties.Item("Creation Date").Value
Write-Host "Get: Creation Date = $creationDate"
} catch {
$creationDate = "N/A"
Write-Host "Get: Creation Date not found, setting to N/A"
}
try {
$author = $properties.Item("Author").Value
Write-Host "Get: Author = $author"
} catch {
$author = "N/A"
Write-Host "Get: Author not found, setting to N/A"
}
try {
$lastAuthor = $properties.Item("Last Author").Value
Write-Host "Get: Last Author = $lastAuthor"
} catch {
$lastAuthor = "N/A"
Write-Host "Get: Last Author not found, setting to N/A"
}
$values += ,("Creation Date", $creationDate)
$values += ,("Author", $author)
$values += ,("Last Edited By", $lastAuthor)
# カスタムプロパティを設定する関数
function Set-OfficeDocCustomProperty {
[OutputType([boolean])]
Param
(
[Parameter(Mandatory=$true)]
[string] $PropertyName,
[Parameter(Mandatory=$true)]
[string] $Value,
[Parameter(Mandatory=$true)]
[System.__ComObject] $Document
)
try
{
$customProperties = $Document.CustomDocumentProperties
$binding = "System.Reflection.BindingFlags" -as [type]
[array]$arrayArgs = $PropertyName,$false, 4, $Value
try
{
[System.__ComObject].InvokeMember("add", $binding::InvokeMethod,$null,$customProperties,$arrayArgs) | out-null
}
catch [system.exception]
{
$propertyObject = [System.__ComObject].InvokeMember("Item", $binding::GetProperty, $null, $customProperties, $PropertyName)
[System.__ComObject].InvokeMember("Delete", $binding::InvokeMethod, $null, $propertyObject, $null)
[System.__ComObject].InvokeMember("add", $binding::InvokeMethod, $null, $customProperties, $arrayArgs) | Out-Null
}
return $true
}
catch
{
return $false
}
}
# カスタムプロパティを読み取る関数
function Get-OfficeDocCustomProperty {
[OutputType([string], $null)]
Param
(
[Parameter(Mandatory=$true)]
[string] $PropertyName,
[Parameter(Mandatory=$true)]
[System.__ComObject] $Document
)
try {
$comObject = $Document.CustomDocumentProperties($PropertyName)
$binding = "System.Reflection.BindingFlags" -as [type]
$val = [System.__ComObject].invokemember("value",$binding::GetProperty,$null,$comObject,$null)
return $val
} catch {
return $null
}
}
# カスタムプロパティを設定
$customPropertyName = "Project"
$customPropertyValue = "FA"
Set-OfficeDocCustomProperty -PropertyName $customPropertyName -Value $customPropertyValue -Document $doc
# カスタムプロパティを読み取る
$customPropertyValueRead = Get-OfficeDocCustomProperty -PropertyName $customPropertyName -Document $doc
$values += ,($customPropertyName, $customPropertyValueRead)
# プロパティの値を新しいセクションに追加
$range = $newSection.Range
foreach ($value in $values) {
$range.InsertAfter("$($value[0]): $($value[1])`n")
}
# Wordファイルを保存して閉じる
$doc.Save()
$doc.Close()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($doc) | Out-Null
$word.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($word) | Out-Null
[gc]::collect()
[gc]::WaitForPendingFinalizers()
Write-Host "Ready!" -ForegroundColor Green