none
PowerShell: Store Entire Workbook as a pdf file RRS feed

  • Question

  • Hello togehter,

    i am search for the property to store a xlsx document with the entire workbook  as a pdf.

    This is my script. But i didn't find a property to set the option.

    $path = "C:\Users\Downloads\" 
    $xlFixedFormat = "Microsoft.Office.Interop.Excel.xlFixedFormatType" -as [type] 
    $excelFiles = Get-ChildItem -Path $path -include *.xls, *.xlsx -recurse 
    $objExcel = New-Object -ComObject excel.application 
    $objExcel.visible = $true 
    foreach($wb in $excelFiles) 
    { 
     $filepath = Join-Path -Path $path -ChildPath ($wb.BaseName + ".pdf") 
     $workbook = $objExcel.workbooks.open($wb.fullname, 3) 
     $workbook.Saved = $true 
    "saving $filepath" 
     $workbook.ExportAsFixedFormat($xlFixedFormat::xlTypePDF,$xlFixedFormat::xlprintall, $filepath) 
     $objExcel.Workbooks.close() 
    } 
    $objExcel.Quit()

    i mean this option with storing excel as a pdf file

    Thank's a lot for your replys.

     

    Tuesday, November 17, 2015 9:54 AM

Answers

  • Hello together,

    after search and trying the next 6 hours i found the solution

    here is the script:

    $path = "C:\Users\Downloads" 
    $xlFixedFormat = "Microsoft.Office.Interop.Excel.xlFixedFormatType" -as [type] 
    $excelFiles = Get-ChildItem -Path $path -include *.xls, *.xlsx -recurse 
    $objExcel = New-Object -ComObject excel.application 
    $objExcel.visible = $false 
    foreach($wb in $excelFiles) 
    { 
     $filepath = Join-Path -Path $path -ChildPath ($wb.BaseName + ".pdf") 
     $workbook = $objExcel.workbooks.open($wb.fullname, 3) 
     $workbook.ActiveSheet.PageSetup.Orientation = 2
     $objExcel.PrintCommunication = $false
     $workbook.ActiveSheet.PageSetup.FitToPagesTall = $false
     $workbook.ActiveSheet.PageSetup.FitToPagesWide = 1
     $objExcel.PrintCommunication = $true
     $workbook.Saved = $true 
    "saving $filepath" 
     $workbook.ExportAsFixedFormat($xlFixedFormat::xlTypePDF, $filepath) 
     $objExcel.Workbooks.close() 
    } 
    $objExcel.Quit()

    Tuesday, November 17, 2015 4:12 PM