Quantcast
Channel: Excel IT Pro Discussions forum
Viewing all articles
Browse latest Browse all 11829

XLS to CSV via powershell

$
0
0

HI,

I am doing the following to convert a xls file to csv file:

$tmp = C:\programdata\download.xls
$objExcel = New-Object -ComObject Excel.Application 
    if ((test-path $tmp) -and ($tmp -match ".xl\w*$")) 
        {
        $path = (resolve-path -Path $tmp).path 
        $savePath = $tmp -replace ".xl\w*$",".csv"
        if(Test-path $savePath)
            {
            Remove-Item -Path $savePath -Force | Out-Null
            }
        $objworkbook=$objExcel.Workbooks.Open($tmp)
        $objworkbook.SaveAs($savePath,6) # 6 is the code for .CSV 
        $objworkbook.Close($false) 
        }

Doing this normal via powershell it works fine
But if I use a schedule Task using

powershell.exe -file C:\data\myscript.ps1 -executionpolicy Unrestricted

the csv is not created.
I already tried I have write rights to the folder and the script is started correctly.

Any idea why this could fail?
By printing out $objExcel > $env:APPDATA\objExcel.txt I get a txt with type of $objExcel
By printing out $objworkbook > $env:APPDATA\objWorkbook.txt I get only an empty objWorkbook.txt :-(

Could this be a problem?

Thank you for help

MK-Maddin


Viewing all articles
Browse latest Browse all 11829

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>