Link to home
Start Free TrialLog in
Avatar of cbones
cbonesFlag for United States of America

asked on

Script to edit margins in word

Is there a script that can be scheduled to check a
directory and change the margins of word files in that folder?

Thank you
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

You need the Word application, and with that you can use VBA to edit documents.

Here are a couple of VBA macros.
Sub CallEditFiles()
    EditFiles "C:\MyFolder", "*.doc*"
End Sub

Sub EditFiles(strFolder As String, strFilePattern As String)
    Dim strFileName As String
    Dim doc As Document
    
    strFileName = Dir$(strFolder & "\" & strFilePattern)
    Do Until strFileName = ""
        DoEvents
        Set doc = Documents.Open(strFolder & "\" & strFileName)
        doc.PageSetup.LeftMargin = CentimetersToPoints(1)
        doc.PageSetup.RightMargin = CentimetersToPoints(1)
        doc.PageSetup.TopMargin = CentimetersToPoints(1.5)
        doc.PageSetup.BottomMargin = CentimetersToPoints(1.5)
        
        doc.Close wdSaveChanges
        strFileName = Dir$()
    Loop
End Sub

Open in new window

Avatar of cbones

ASKER

When I run this I received the following error:

Script:         c:\test\macro.vbs
Line:            5
Char:           25
Error:           Expected ')'
Code:          800A03EE
Source:       Microsoft VBScript compilation error

Ths't because it's VBA code. Must it be VBScript?
Avatar of cbones

ASKER

If that is possible, yes.  
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cbones

ASKER

The error message that comes up is:

Script: "c:\test.vbs"
Line:    22
Char:   13
Error:   Type mismatch: 'CentimetersToPoints'
Code:   800A000D
Source:  Microsoft VBScript runtime error