Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing Margins on Multiple Word Documents

Status
Not open for further replies.

cjh1

Technical User
Jun 17, 2003
30
0
0
GB
We have recently changed our letterheaded paper which means that the margins on our standard Word 97 documents need to be changed. A few users here were wondering if there was any way of changing the margins on the documents all at once rather than having to open each document, change the margins then save.

Does anyone know if this can be done?

Thanks,

Caroline
 
Hi,
This can be done with Word VBA. This procedure starts with an Open Window so that you can select a Word document in a folder. The procedure will continue to open ALL THE WORD DOCUMENTS in that folder and change the margins.

Here's what you need to do.

1. start Word
2. alt+F11 -- opens the VB Editor
3. ctl+R -- opens the Project Explorer
4. in Project Explorer open the Normal Object and Insert a Module
5. copy these two procedures into your new Module
6. change the appropriate Margin values in ChangeMargins. In fact there may be even a better way to define these values. Turn on your Macro Recorder and record the Page Setup that you want for all your documents. Then go into the VB Editor and change the name of new Macro to ChangeMargins and just delete my ChangeMargins macro.

Now you are ready to chage margins. I would suggest that you set up a test folder with 2 or 3 Word documents and run the test on them to see that everything comes out OK.

Here's how to run.

1. Go to your document
2. alt+F8
3. Select OpenDocuments

Hope this helps :)
Code:
Sub OpenDocuments()
    Set xl = CreateObject("Excel.Application")
    Set fs = Application.FileSearch
    With fs
        FileName = xl.GetOpenFilename("Word Files (*.doc), *.doc")
        For i = Len(FileName) To 1 Step -1
            If Mid(FileName, i, 1) = "\" Then
                Exit For
            End If
        Next
        .LookIn = Left(FileName, i - 1)
        .FileName = "*.doc"
        If .Execute(SortBy:=msoSortByFileName, _
        SortOrder:=msoSortOrderAscending) > 0 Then
            MsgBox "There were " & .FoundFiles.Count & _
                " file(s) found."
            i = 1
            For i = 1 To .FoundFiles.Count
                Documents.Open FileName:=.FoundFiles(i)
                ChangeMargins
                ActiveDocument.Save
                ActiveDocument.Close
            Next i
        Else
            MsgBox "There were no files found."
        End If
    End With
End Sub
Sub ChangeMargins()
    With ActiveDocument.PageSetup
        .TopMargin = InchesToPoints(1)
        .BottomMargin = InchesToPoints(1)
        .LeftMargin = InchesToPoints(1)
        .RightMargin = InchesToPoints(1)
        .Gutter = InchesToPoints(0)
        .HeaderDistance = InchesToPoints(0.5)
        .FooterDistance = InchesToPoints(0.5)
    End With
End Sub


Skip,
Skip@TheOfficeExperts.com
 
Skip,

Thanks for the code it has worked a treat. Whats the best way to get this to other users so that they can change their own margin settings?

Cheers,

Caroline
 
1. Drag the module that has your 2 macros to a document object.
2. Paste the "how to run" instructions into the document.
3. Save the document
4. Distribute the document to others.

Skip,
Skip@TheOfficeExperts.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top