How can I convert Excel documents (files) to PDF in an automated fashion? I am trying to adapt the solution. So far I have this:
var fso = new ActiveXObject("Scripting.FileSystemObject");
var docPath = WScript.Arguments(0);
docPath = fso.GetAbsolutePathName(docPath);
var pdfPath = docPath.replace(/\.xls[^.]*$/, ".pdf");
var objExcel = null;
try
{
WScript.Echo("Saving '" + docPath + "' as '" + pdfPath + "'...");
objExcel = new ActiveXObject("Excel.Application");
objExcel.Visible = false;
var objExcel = objExcel.Workbooks.Open(docPath);
var wdFormatPdf = 17;
objExcel.SaveAs(pdfPath, wdFormatPdf);
objExcel.Close();
WScript.Echo("Done.");
}
finally
{
if (objExcel != null)
{
objExcel.Quit();
}
}
Output:
Saving 'somefile.xlsx' as 'somefile.pdf'...
Done.
..\SaveXLSXAsPDF.js(27, 9) (null): The object invoked has disconnected from its clients.
An erroneous PDF file is created, and it will not open in a reader. Perhaps format 17 isn't the best choice for Excel, I'm thinking. Does anyone have the correct information or know how to make this work?