如何將 MS Word 檔案從 Letter 頁面大小批次轉換為 A4?

如何將 MS Word 檔案從 Letter 頁面大小批次轉換為 A4?

我有一堆 MS Word 2010 文檔,我需要將它們從 Letter 頁面大小轉換為 A4。有沒有一種簡單的方法可以做到這一點?可能是某些 PowerShell 腳本與一些 MS Word API 結合?

答案1

以下是一些 VBA,您可以新增為巨集來變更給定資料夾中的所有 Word 文件。

警告:在運行此程式碼之前先備份檔案。

開啟一個新的 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()

相關內容