Solved

Script to edit margins in word

Posted on 2011-02-28
6
241 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

622 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