I'm trying to convert an Excel .xls file that has several worksheets into a .csv with Powershell 4.0. I know the SaveAs in the for each loop isn't phrased right, and that the error is pointing to line 17 and character 9, I just don't know how to fix it or how to interpret the error code 0x800A03EC.
Here's the script:
Function ExportWSToCSV ($inputWorkbookPath, $outputFilePrefix, $outputDirectory)
{
#Start Excel invisibly without pop-up alerts.
$inputWorkbookPath = "R:\Unclaimed Property\NC State\Jun 2015\" + `
"NC_RAW_JUL1986thruMAR2013" + ".xls"
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $false
$excel.DisplayAlerts = $false
#Open Excel file.
$workBook = $excel.Workbooks.Open($inputWorkbookPath)
foreach ($workSheet in $workBook.Worksheets)
{
$n = $inputWorkbookPath + "_" + $workSheet.Name
$workSheet.SaveAs($outputDirectory + $n + ".csv", 6)
}
$excel.Quit()
}
ExportWSToCSV -inputWorkbookPath "R:\Unclaimed Property\NC State\Jun 2015\NC_RAW_JUL1986thruMAR2013.xls" `
-outputFilePrefix "output_" `
-outputDirectory "R:\Unclaimed Property\NC State\Jun 2015\"
Here's the error:
Exception calling "SaveAs" with "2" argument(s): "Exception from HRESULT: 0x800A03EC"
At \\ncdfs1\documents$\ANDREWN\My Documents\PSscript_for_NC.ps1:17 char:9
+ $workSheet.SaveAs($outputDirectory + $n + ".csv", 6)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ComMethodTargetInvocation
$nbut then you add the path again with$outputDirectory. I suggest you put in the loop just to see$outputDirectory + $n + ".csv"what that output looks like. You should see an awful looking path. Are you able to just remove$inputWorkbookPath + "_" +from$n?.SaveAs()method works fine. It's your generated path that's probably a bit shonky.