Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Script to edit margins in word

Posted on 2011-02-28
6
Medium Priority
?
243 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

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

Accepted Solution

by:
GrahamSkan earned 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

704 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