The team needed to convert a big number (200+) of Word documents saved in different formats into a single format, DOTX to be specific.
You have access to a Windows machine with Microsoft Office 2010 installed.
- Open up a text editor, paste the code in Listing 1, and save the file as bulk_word_exporter.js.
- Set the value of sourcePath (line 10) to the folder containing the source files. Use double backslash to separate the folders.
In my example different file types (DOC, DOCX, DOT, etc.) are stored in different source folders and that’s why extension is added to the sourcePath. Feel free to change this.
- Set the value of destPath (line 11) to the folder where you want the exported files to be saved. Create the folder if it doesn’t exist.
- open command line and run the script:
This code utilizes Scripting.FileSystemObject library to access the file system:
13. fso = new ActiveXObject("Scripting.FileSystemObject");
It also uses Word.Application for opening and saving Word documents:
15. wordApp = new ActiveXObject("Word.Application");
Note: You can refactor and move the whole extension setting part to the command line arguments. That would be more elegant.
Set extension to ‘PDF’ (line 7) and fileFormat (line 8) to 17 and run the code. It will bulk export a batch of Word documents to PDFs. This could be a life saver if you deal with managing information and records.
Other Tools of the Trade
Don’t forget about MS-Excel and MS-Access when you;re working on more serious automation projects. Both applications provide structured data storage and offer programming using VBA. You can utilize many libraries and do really cool things with these tools.
Efficiency as a goal
When my colleagues ask me how long it will take for me to automate job X by writing a script, I always ask them how long will it take if we do it manually?
Writing scripts and automating work is fun and rewarding, but some tasks, no matter how mundane, are done faster manually. The main goal here is to improve efficiency, not to write code. So always consider the faster route.
However, if you are about to repeat the same manual process a few times a month or even a few times a year, it is worth spending some time and automating it.
Another added value of automation is when you are not around, or when you’ve moved on (to a better position), others will still be able to automate mundane work thanking you for the years to come. Trust me, you’ll be thanked for some time.