How to batch-convert MS Word files from Letter page size to A4?

1

1

I have a bunch of MS Word 2010 documents and I need to convert them from Letter page size to A4. Is there a simple way to do that? Possibly some PowerShell script combined with some MS Word API?

Borek Bernard

Posted 2012-09-27T09:50:23.103

Reputation: 11 400

Answers

5

Here is some VBA you can add as a macro to change all the Word documents in a given folder.

WARNING: Make a backup copy of your files prior to running this code.

Open a new Word document, paste this code into the VBA window (Alt+F11). Make necessary changes to the path, then close the window.

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

Open the Macro window (Alt+F8) and choose ChangePaperSize, then click run. The current open document will close and other documents will open and close as it makes the changes to each document in the folder.

CharlieRB

Posted 2012-09-27T09:50:23.103

Reputation: 21 303

There was a feedback (deleted by review) that "Documents.Close ..." has to be commented out or the macro would stop with the closure of the document. Otherwise everything works fine and OP expressed his gratitude to you for that. – guest-vm – 2018-05-25T15:08:35.957

Excellent, thanks, that gave me enough clues to write the same thing in PowerShell (which I find easier to use). Will post the code as separate answer. – Borek Bernard – 2012-09-27T19:02:58.143

Great. Glad to help. – CharlieRB – 2012-09-27T19:13:50.357

1

PowerShell version based on CharlieRB's answer:

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()

Borek Bernard

Posted 2012-09-27T09:50:23.103

Reputation: 11 400