Solved

Script to edit margins in word

Posted on 2011-02-28
6
234 Views
Last Modified: 2012-06-21
Is there a script that can be scheduled to check a
directory and change the margins of word files in that folder?

Thank you
0
Comment
Question by:cbones
  • 3
  • 3
6 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 35000667
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

0
 

Author Comment

by:cbones
ID: 35000853
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

0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 35003198
Ths't because it's VBA code. Must it be VBScript?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:cbones
ID: 35003374
If that is possible, yes.  
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 35003422
I usually struggle with VBScript, but try this.
Dim strFolder

Dim doc 'As Word.Document
Dim fso 'As FileSystemObject
Dim fld 'As Folder
Dim fl 'As File
Dim p 'As Integer
Dim wdApp 'As Word.Application

strFolder = "C:\MyFolder"

Set fso = CreateObject("Scripting.FileSystemObject")
Set wdApp = CreateObject("Word.Application")
wdApp.Visible = True

Set fld = fso.GetFolder(strFolder)
For Each fl In fld.Files
    p = InStr(fl.Name, ".")
    Select Case Mid(fl.Name, p)
        Case ".doc", ".docx", ".docm"
            Set doc = wdApp.Documents.Open(fl.Path)
            doc.PageSetup.LeftMargin = CentimetersToPoints(1)
            doc.PageSetup.RightMargin = CentimetersToPoints(1)
            doc.PageSetup.TopMargin = CentimetersToPoints(1.5)
            doc.PageSetup.BottomMargin = CentimetersToPoints(1.5)
            doc.Close -1 'wdSaveChanges
     End Select
wdApp.Quit
Next

Open in new window

0
 

Author Comment

by:cbones
ID: 35010095
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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you work with Word a lot, you probably use styles. If you use styles a lot, you've probably balled your fist more often than not when working with the ribbon. In Word 2007/2010, one of the things that I find missing when using styles is a quic…
The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now