Converting a Word document to PDF file is simple and easy. Just open Word document in Ms Word and save as PDF. But what if you have tons of Word files to convert into PDF? Opening each file manually and saving it to PDF will take ages. But using the following trick, you can bulk convert Word to PDF in just one click. And that too using no external tool. You just need Ms Word.
Requirement
Only Ms Word. Yes, you the only requirement is MS Word with capability to save document as PDF. For Ms Word 2010 and above, this feature is already available. However, for Ms Word 2007 or lower, you need to install “Microsoft Save as PDF or XPS” plugin.
Step to Bulk Convert Word to PDF
Step 1: Organizing word files
Move all your Ms Word documents, that needs to be converted to PDF in one folder say “fldr“. You can name it anything you like.
Step 2: Code to convert Word to PDF
Open notepad/notepad++ and copy the following code and save file as “SaveAsPDF.js” in a folder say“fldr”. Note: Don’t use Ms Word to save it.
Following code converts Word document to PDF file.
var obj = new ActiveXObject("Scripting.FileSystemObject");
var docPath = WScript.Arguments(0);
docPath = obj.GetAbsolutePathName(docPath);
var pdfPath = docPath.replace(/\.doc[^.]*$/, ".pdf");
var objWord = null;
try
{
objWord = new ActiveXObject("Word.Application");
objWord.Visible = false;
var objDoc = objWord.Documents.Open(docPath);
var format = 17;
objDoc.SaveAs(pdfPath, format);
objDoc.Close();
WScript.Echo("Saving '" + docPath + "' as '" + pdfPath + "'...");
}
finally
{
if (objWord != null)
{
objWord.Quit();
}
}
Step 3: Create Batch file to bulk convert Word to PDF
Open notepad or notepad++ and copy the following code. Save the file with “.bat” extension say “bulk-convert-Word2PDF.bat” in folder “fldr”.
echo off
for %%X in (*.docx) do cscript.exe //nologo SaveAsPDF.js "%%X"
for %%X in (*.doc) do cscript.exe //nologo SaveAsPDF.js "%%X"
Step 4: Running Batch file
Double click batch file “bulk-convert-Word2PDF.bat” created in above step and relax. All the word documents with .docx and .doc extension in folder “fldr” is saved to the pdf file with the same name.
Summary
Simple code shown in the blog helps automate task of converting Ms Word document into pdf files. To bulk convert Word to PDF, just double click on “bulk-convert-Word2PDF.bat” and Relax!!!
Related Posts
C P Gupta is a YouTuber and Blogger. He is expert in Microsoft Word, Excel and PowerPoint. His YouTube channel @pickupbrain is very popular and has crossed 9.9 Million Views.
I get this error
C:\Users\thams\Desktop\RCID proposals>01
’01’ is not recognized as an internal or external command,
operable program or batch file.
C:\Users\thams\Desktop\RCID proposals>echo off
’02’ is not recognized as an internal or external command,
operable program or batch file.
C:\Users\thams\Desktop\RCID proposals\SaveAsPDF.js(17, 1) Microsoft JScript compilation error: Expected ‘{‘
C:\Users\thams\Desktop\RCID proposals\SaveAsPDF.js(17, 1) Microsoft JScript compilation error: Expected ‘{‘
C:\Users\thams\Desktop\RCID proposals\SaveAsPDF.js(17, 1) Microsoft JScript compilation error: Expected ‘{‘
Ohoo. It works fine for me and many more. For more details on exact procedure, please see video https://youtu.be/XLzxA1t7kTQ
You have to remove the line numbers that get copied across:
var obj = new ActiveXObject(“Scripting.FileSystemObject”);
var docPath = WScript.Arguments(0);
docPath = obj.GetAbsolutePathName(docPath);
var pdfPath = docPath.replace(/\.doc[^.]*$/, “.pdf”);
var objWord = null;
try
{
objWord = new ActiveXObject(“Word.Application”);
objWord.Visible = false;
var objDoc = objWord.Documents.Open(docPath);
var format = 17;
objDoc.SaveAs(pdfPath, format);
objDoc.Close();
WScript.Echo(“Saving ‘” + docPath + “‘ as ‘” + pdfPath + “‘…”);
}
finally
{
if (objWord != null)
{
objWord.Quit();
}
}
Same with the batch file.
Copy and paste the text to excel first. Excel will automatically separate the line numbers from the code into two separate columns. You can then copy only the second column back into notepad without the line numbers. This is very quick and easy. Otherwise, just delete the line numbers.
Thanks dear for providing the solution. I will try to permanently fix it using some good plugin from my end.
Thanks for the comment. Happy to help!!!
Hi, Thanks, I have some errors when I want to add other extensions as xls and ppt. Can you please help? Thanks
Input Error: There is no script engine for file extension “.js”.
Getting this error
Add /R to the bat script to recursively process doc files in multiple folders/directories.
echo off
for /R %%X in (*.docx) do cscript.exe //nologo SaveAsPDF.js “%%X”
for /R %%X in (*.doc) do cscript.exe //nologo SaveAsPDF.js “%%X”
Thanks very much. This will be useful for all visitors.
Thanks dear for providing us with super useful information. This will be of great use to all.
Great it really work, Thanks
Welcome dear. Glad to know it was useful.
I get this error when i run the batch file
Input Error: There is no script engine for file extension “.js”.
Pingback: WORD hacks – NVRMND
Could I have the version for excel and ppt please
I have tried, it works on Office 2016 and above. In lower version also it may work.