Created
November 24, 2013 07:20
-
-
Save shabdar/7624346 to your computer and use it in GitHub Desktop.
A Windows command line script written in JavaScript for bulk exporting Word documents to other formats (DOT, PDF, etc.)
Run with CScript.exe. Tested in Windows 7 with MS-Office 2010 installed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
alert = function(s) { WScript.Echo(s) } | |
var fso, | |
folder, | |
wordDoc, | |
wordApp, | |
extension = 'DOT', | |
fileFormat = 1, //see http://msdn.microsoft.com/en-us/library/office/ff839952.aspx | |
sourcePath = "\\SOURCE\\PATH\\" + extension + "\\", | |
destPath = "\\DESTINATION\\PATH\\"; | |
try { | |
fso = new ActiveXObject("Scripting.FileSystemObject"); | |
wordApp = new ActiveXObject("Word.Application"); | |
wordApp.Visible = false; | |
folder = fso.GetFolder(sourcePath); | |
files = new Enumerator(folder.files); | |
alert("Preparing ..."); | |
var fileURI, fileName; | |
for(var files = new Enumerator(folder.files); !files.atEnd(); files.moveNext()) { | |
fileURI = '' + files.item(); | |
fileName = fileURI.substr(fileURI.lastIndexOf("\\") + 1, fileURI.length - | |
fileURI.lastIndexOf("\\") - (fileURI.length - fileURI.lastIndexOf(".") + 1)); | |
alert("exporting '" + fileName + "'..."); | |
wordDoc = wordApp.Documents.open(fileURI); | |
alert("opened '" + fileName + "'..."); | |
wordDoc.SaveAs(destPath + fileName + "." + extension, fileFormat); | |
wordDoc.close(); | |
alert("exported '" + fileName + "' to " + extension); | |
} | |
alert("\n\nDone!\n"); | |
} catch (error) { | |
alert(serialize(error)); | |
} finally { | |
//close handles and cleanup the memory | |
fso = null; | |
if (wordDoc) { | |
wordDoc.Close(0); | |
} | |
if (wordApp) { | |
wordApp.Quit(); | |
} | |
wordDoc = null; | |
wordApp = null; | |
fso = null; | |
} | |
/* serialize function, thanks to http://blog.stchur.com/2007/04/06/serializing-objects-in-javascript/ */ | |
function serialize(_obj) | |
{ | |
// Let Gecko browsers do this the easy way | |
if (typeof _obj.toSource !== 'undefined' && typeof _obj.callee === 'undefined') | |
{ | |
return _obj.toSource(); | |
} | |
// Other browsers must do it the hard way | |
switch (typeof _obj) | |
{ | |
// numbers, booleans, and functions are trivial: | |
// just return the object itself since its default .toString() | |
// gives us exactly what we want | |
case 'number': | |
case 'boolean': | |
case 'function': | |
return _obj; | |
break; | |
// for JSON format, strings need to be wrapped in quotes | |
case 'string': | |
return '\'' + _obj + '\''; | |
break; | |
case 'object': | |
var str; | |
if (_obj.constructor === Array || typeof _obj.callee !== 'undefined') | |
{ | |
str = '['; | |
var i, len = _obj.length; | |
for (i = 0; i < len-1; i++) { str += serialize(_obj[i]) + ','; } | |
str += serialize(_obj[i]) + ']'; | |
} | |
else | |
{ | |
str = '{'; | |
var key; | |
for (key in _obj) { str += key + ':' + serialize(_obj[key]) + ','; } | |
str = str.replace(/\,$/, '') + '}'; | |
} | |
return str; | |
break; | |
default: | |
return 'UNKNOWN'; | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment