Reputation: 303
This is my first time working with PowerShell script. I want to convert first sheet of worksbooks to pdf. I got the code below which converts all worksheets of workbooks to pdf. How can I change this code to convert only first worksheet to pdf? Please help.
$path = "C:\Users\addns\Desktop\Template"
$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, 1)
$workbook.Saved = $true
"saving $filepath"
$workbook.ExportAsFixedFormat($xlFixedFormat::xlTypePDF, $filepath)
$objExcel.Workbooks.close()
}
$objExcel.Quit()
Upvotes: 1
Views: 1333
Reputation: 2921
You can use $workbook.WorkSheets.Item(1)
to get the first worksheet in $workbook
, and then call your export function on that.
Upvotes: 1