PowerShell 技能连载 - 将 Word 文档从 .doc 格式转为 .docx 格式(第 1 部分)

将旧式的 Word 文档转为新的 .docx 格式需要比较多工作量。多谢 PowerShell,您可以自动完成该转换工作:

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
function Convert-WordDocument
{
param
(
[Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[string]
[Alias('FullName')]
$Path
)
begin
{
# launch Word
$word = New-Object -ComObject Word.Application
}

process
{
# determine target path
$pathOut = [System.IO.Path]::ChangeExtension($Path, '.docx')

# open the document
$doc = $word.Documents.Open($Path)

Write-Progress -Activity 'Converting' -Status $PathOut

# important: do the actual conversion
$doc.Convert()

# save in the appropriate format
$doc.SaveAs([ref]$PathOut,[ref]16)

# close document
$word.ActiveDocument.Close()
}
end
{
# close word
$word.Quit()
}
}
评论