-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathConvert Excel to PDF.ps1
33 lines (29 loc) · 1.1 KB
/
Convert Excel to PDF.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
Function Excel2PDF{
Param (
<#
.PARAMETER NewName = DIRECTORY that houses the excel files. This also is the output directory of the pdf files.
#>
[Parameter(Mandatory=$True,Position=0)]
[String]$path
)
$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.Saved = $true
"saving $filepath"
$workbook.ExportAsFixedFormat($xlFixedFormat::xlTypePDF, $filepath)
$objExcel.Workbooks.close()
}
Function Execute-CleanUp
{
$Workbook.Close($false) | Out-Null;
$x1.Quit() | Out-Null;
(Get-Process -Name Excel) | Where-Object { $_.MainWindowHandle -eq $X1.HWND } | Stop-Process | Out-Null;
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($X1) | Out-Null;
}
}