As some background, this should take an excel file, and convert it to PDF (and place the PDF into a temporary folder).
E.g. 'C:UsersgjacobsDesktopeststock.xlsx'
becomes
'C:UsersgjacobsDesktopestpdf_merge_tmpstock.pdf'
However, the new file path does not return correctly.
If I echo the string $export_name from within the function, I can see that it has the correct value: "C:UsersgjacobsDesktopestpdf_merge_tmpstock.pdf".
But once $export_name is returned, it has a different (incorrect value): "C:UsersgjacobsDesktopestpdf_merge_tmp C:UsersgjacobsDesktopestpdf_merge_tmpstock.pdf".
function excel_topdf{
param(
$file
)
#Get the parent path
$parent = Split-Path -Path $file
#Get the filename (no ext)
$leaf = (Get-Item $file).Basename
#Add them together.
$export_name = $parent + "pdf_merge_tmp" + $leaf + ".pdf"
echo ($export_name) #prints without issue.
#Create tmp dir
New-Item -Path $parent -Name "pdf_merge_tmp" -ItemType "Directory" -Force
$objExcel = New-Object -ComObject excel.application
$objExcel.visible = $false
$workbook = $objExcel.workbooks.open($file, 3)
$workbook.Saved = $true
$xlFixedFormat = “Microsoft.Office.Interop.Excel.xlFixedFormatType” -as [type]
$workbook.ExportAsFixedFormat($xlFixedFormat::xlTypePDF, $export_name)
$objExcel.Workbooks.close()
$objExcel.Quit()
return $export_name
}
$a = excel_topdf -file 'C:UsersgjacobsDesktopeststock.xlsx'
echo ($a)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…