Jump to content
  • entries
    2
  • comments
    8
  • views
    197

Making word files smaller


A common problem with Word documents is that people include bitmap images. This can mean that a page with a small amount of text that ought to be no more than a few kilobytes actually takes up megabytes of space.

 

You can try and persuade people not to do this but sometimes it's easier to just compress the files for them.

 

Fortunately, Microsoft make this easy - Office 2007 introduced the .docx format which essentially zips up the document. If it has a bitmap in it, this will normally make the file much, much smaller.

 

Even if users are working with Office 2003 or Office XP they'll still be able to edit the files as long as they have the file format converters installed - you can push these out with WSUS if that's not already been done.

 

The script below will trawl through a directory tree and check for .doc files greater than a certain size and older than a given date. If it finds them, it opens the file in Word, resaves it as .docx and once that's safely done, deletes the original file.

 

'this is the folder to process
sFolder="T:\PDSU"
'files newer than this number of days won't be changed
iDays=30


dim oWord
Const wdFormatDocumentDefault             = 16
Const Megabyte=1000000
set oFSO=createobject("scripting.filesystemobject")
ProcessFolder sFolder

Sub Processfolder(sFolder)
 wscript.echo sFolder
 set oFolder=ofso.getfolder(sFolder)
 set oTopFolder=oFolder.parentfolder
 sTopFolder=oTopFolder.path
 for each oFile in oFolder.files
   if oFile.size > 2 * Megabyte then
     if datediff("d",oFile.datelastmodified,now)>iDays then
       if ucase(ofso.GetExtensionName(oFile))="DOC" then
         sBase=oFSO.GetBaseName(oFile)
         Doc2DocX sFolder,sBase
       end if
     end if
   end if
 next
 for each oSubFolder in oFolder.subfolders
   ProcessFolder sFolder & "\" & oSubFolder.name
 next
end sub

Sub Doc2DocX( sFolder, sFile )
 ' Build the fully qualified DOCX file name
 sOutputFile = sFolder & "\" & sFile & ".docx"
 sInputFile  = sFolder & "\" & sFile & ".doc"
 ' Create a Word object
 Set oWord = CreateObject( "Word.Application" )
 With oWord
   ' True: make Word visible; False: invisible - makes it faster becaus no screen redraw
   .Visible = false
   ' Open the Word document
   .Documents.Open sInputFile
   ' Make the opened file the active document
   Set oDoc = .ActiveDocument
   ' Save as DOCX
   oDoc.SaveAs sOutputFile, wdFormatDocumentDefault
   wscript.echo sOutputFile
   ' Close the active document
   oDoc.Close
   'quit Word
   .quit
 End With
 if ofso.fileexists(sOutputFile) then
   wscript.sleep 1000 'wait for Word to close
   ofso.deletefile sInputFile,true
 end if
End Sub

8 Comments


Recommended Comments

p858snake

Posted

You can also:

 

Right click a image, go into format image, a few tabs over, compress image and select the "compress all" option.

srochford

Posted

The GUI method in Word is good if it's your document and you've only got 1 or 2 of them - I should have made the point that the script was for when you're running a network which has hundreds or thousands of DOC files containing images which need compressing. The automation in this case is vital if you're going to get the job done in a sensible time!
browolf

Posted

I didn't know you could push out the file format converters with wsus!
DSapseid

Posted

is there something similar for rtf's? ive just done a scan of the network for large files and one teacher has about 100 rtf's that are huge!! i have saved one as a docx and its gone from 100mb to about 1.5mb
srochford

Posted

Change the line which says:

if ucase(ofso.GetExtensionName(oFile))="DOC" then

to

if ucase(ofso.GetExtensionName(oFile))="DOC" or ucase(ofso.GetExtensionName(oFile))="RTF" then

 

and you will process DOC and RTF files.

Cools

Posted

just need to compress the 7mb jpg's in the ppt and pub files the kids and staff create.
srochford

Posted

Not sure how to do this for Publisher - even Publisher 2010 doesn't seem to compress images when it saves - but powerpoint from at least 2007 seems to compress the images by default (my test image is 4Mb iSaving this n a .doc file gives a 5Mb document but .docx drops to just 600kb. The pptx file is just over 1.1Mb but the ppt file is only a fraction larger.

 

It's got to be possible to script a process that opens a .pub file, selects each image in turn, selects "compress image" and then saves the result - it's not as good as the compression in Word but the defaults do shrink by about 50% which is better than nothing!

browolf

Posted

kixtart scripting is good for pressing buttons/shortcuts

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...