Solved

Script to edit margins in word

Posted on 2011-02-28
6
227 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

747 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

11 Experts available now in Live!

Get 1:1 Help Now