title: Creating a powershell script to convert and excel worksheet to pdf
body: <p>I have done some searching and have gotten close but, still don't have a completely working script. Does anyone know what I'm doing wrong and what I need to change or add for this to work correctly.
  Here is the code:</p>
  
  <pre><code>$path = "D:\Geoff's Files"
  
  $dir = "D:\Geoff's Files\*.xls
  
  $xlFixedFormat = Microsft.Office.Interop.Excel.xlFixedFormatType" -as [type]
  
  $latest = Get-ChildItem -Path $dir | Sort-Object LastAccessTime -Descending | Select-Object -First 1
  
  $latest.name
  
  $ExcelFile = "$latest"
  
  $excel = New-Object -ComObject Excel.Application
  
  $excel.visible = $true
  
  $Workbook = $excel.workbooks.open($ExcelFile,2,$True)
  
  $Worksheets = $Workbook.worksheets
  
  $Worksheet = $Workbook.worksheets.Item(1)
  
  $Worksheet.ExportAsFixedFormat($xlFixedFormat::XlTypePDF, $path)
  
  $workbook.Close()
  
  $excel.Quit()
  </code></pre>
  
  <p>it runs but, throws an error in Excel that says: "Document not saved. The document may be open, or an error may have been encountered when saving."</p>