MS Word ファイルをレター ページ サイズから A4 に一括変換するにはどうすればよいですか?

MS Word ファイルをレター ページ サイズから A4 に一括変換するにはどうすればよいですか?

MS Word 2010 ドキュメントが多数あり、それらをレター ページ サイズから A4 に変換する必要があります。これを行う簡単な方法はありますか? PowerShell スクリプトと MS Word API を組み合わせることは可能でしょうか?

答え1

特定のフォルダー内のすべての Word 文書を変更するためにマクロとして追加できる VBA をいくつか示します。

警告: このコードを実行する前に、ファイルのバックアップ コピーを作成してください。

新しい Word 文書を開き、このコードを VBA ウィンドウ ( Alt+ F11) に貼り付けます。パスに必要な変更を加えて、ウィンドウを閉じます。

Sub ChangePaperSize()
Dim myFile As String
Dim myPath As String
Dim myDoc As Document

'Change to the path where your documents are located.
'This code changes ALL documents in the folder.
'You may want to move only the documents you want changed to seperate folder.
myPath = "C:\temp\"

'Closes open documents before beginning
Documents.Close SaveChanges:=wdPromptToSaveChanges

'Set the path with file name for change
myFile = Dir$(myPath & "*.docx")

    Do While myFile <> ""

    'Open the document and make chages
    Set myDoc = Documents.Open(myPath & myFile)
    myDoc.PageSetup.PaperSize = wdPaperA4

    'Close and saving changes
    myDoc.Close SaveChanges:=wdSaveChanges

    'Next file
    myFile = Dir$()
    Loop
    msgbox "Process complete!"    
End Sub

マクロ ウィンドウ ( Alt+ F8)を開きChangePaperSize、 を選択してから、実行をクリックします。現在開いているドキュメントが閉じられ、フォルダー内の各ドキュメントに変更が加えられると、他のドキュメントが開いたり閉じたりします。

答え2

CharlieRB の回答に基づく PowerShell バージョン:

param(
    [parameter(position=0)]
    [string] $Path
)

$docFiles = (Get-ChildItem $Path -Include *.docx,*.doc -Recurse)

$word = New-Object -com Word.Application

foreach ($docFile in $docFiles) {

    $doc = $word.Documents.Open($docFile.FullName)
    $doc.PageSetup.PaperSize = [Microsoft.Office.Interop.Word.WdPaperSize]::wdPaperA4

    $doc.Save()
    $doc.Close()

}

$word.Quit()

関連情報