I am trying to put together a script that will convert several Excel files into PDF files. I found a link to one online that works.
$path = Read-Host -Prompt 'Input Directory Path and Press Enter'
$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()
}
$objExcel.Quit()
If I copy and paste this into a PowerShell window, the program runs as intended. However, when I attempted to make a shortcut to run the program, I get several errors (the file is saved as a .ps1).
This is the path and arguments I made when setting up the shortcut:
C:WindowsSystem32WindowsPowerShellv1.0powershell.exe -noexit -ExecutionPolicy Bypass -File C:[File Path]
This is the error message I get:
At C:Userscbeals.ENVIROTECHDocumentsTestConvertExcelToPDF.ps1:8 char:62
+ $filepath = Join-Path -Path $path -ChildPath ($wb.BaseName + a€?.pdf ...
+ ~
You must provide a value expression following the '+' operator.
At C:Userscbeals.ENVIROTECHDocumentsTestConvertExcelToPDF.ps1:8 char:63
+ ... lepath = Join-Path -Path $path -ChildPath ($wb.BaseName + a€?.pdfa€)
+ ~~~~~~~~~~
Unexpected token 'a€?.pdfa€' in expression or statement.
At C:Userscbeals.ENVIROTECHDocumentsTestConvertExcelToPDF.ps1:8 char:62
+ $filepath = Join-Path -Path $path -ChildPath ($wb.BaseName + a€?.pdf ...
+ ~
Missing closing ')' in expression.
At C:Userscbeals.ENVIROTECHDocumentsTestConvertExcelToPDF.ps1:7 char:1
+ {
+ ~
Missing closing '}' in statement block or type definition.
At C:Userscbeals.ENVIROTECHDocumentsTestConvertExcelToPDF.ps1:8 char:73
+ ... lepath = Join-Path -Path $path -ChildPath ($wb.BaseName + a€?.pdfa€)
+ ~
Unexpected token ')' in expression or statement.
At C:Userscbeals.ENVIROTECHDocumentsTestConvertExcelToPDF.ps1:14 char:1
+ }
+ ~
Unexpected token '}' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ExpectedValueExpression
Why would this be failing?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…