title: Convert Excel to PDF using JavaScript
body: <p>How can I convert Excel documents (files) to PDF in an automated fashion?
  I am trying to adapt the solution found <a href="https://superuser.com/a/28303/212009">here</a> to Excel. So far I have this:</p>
  
  <pre><code>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();
      }
  }
  </code></pre>
  
  <p>I get this output:</p>
  
  <pre><code>Saving 'somefile.xlsx' as 'somefile.pdf'...
  Done.
  ..\SaveXLSXAsPDF.js(27, 9) (null): The object invoked has disconnected from its clients.
  </code></pre>
  
  <p>A PDF file is created but it is invalid and won't open in a reader. I'm thinking that perhaps format 17 isn't the right one for Excel. Does anyone know the right number or how to get this working?</p>
  
  <p>Edit: I found mention of the correct number <a href="http://msdn.microsoft.com/en-us/library/office/bb241279%28v=office.12%29.aspx" rel="nofollow noreferrer">here</a> (57) but now I get this error:</p>
  
  <pre><code>r:\Web\Roman\SaveXLSXAsPDF.js(27, 13) Microsoft JScript runtime error: Class doesn't support Automation
  </code></pre>