-
Notifications
You must be signed in to change notification settings - Fork 20
/
Diff-Word.ps1
39 lines (31 loc) · 1.02 KB
/
Diff-Word.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
param(
[string] $BaseFileName,
[string] $ChangedFileName
)
$ErrorActionPreference = 'Stop'
function resolve($relativePath) {
(Resolve-Path $relativePath).Path
}
$BaseFileName = resolve $BaseFileName
$ChangedFileName = resolve $ChangedFileName
# Remove the readonly attribute because Word is unable to compare readonly
# files:
$baseFile = Get-ChildItem $BaseFileName
if ($baseFile.IsReadOnly) {
$baseFile.IsReadOnly = $false
}
# Constants
$wdDoNotSaveChanges = 0
$wdCompareTargetNew = 2
try {
$word = New-Object -ComObject Word.Application
$word.Visible = $true
$document = $word.Documents.Open($BaseFileName, $false, $false)
$document.Compare($ChangedFileName, [ref]"Comparison", [ref]$wdCompareTargetNew, [ref]$true, [ref]$true)
$word.ActiveDocument.Saved = 1
# Now close the document so only compare results window persists:
$document.Close([ref]$wdDoNotSaveChanges)
} catch {
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.MessageBox]::Show($_.Exception)
}