Powershell Excel Question

i All. I have a script that brings in an excel workbook and exports to CSV. Problem is, i want this to hit multiple workbooks in excel, starting from

$oWorksheet = $oExcelDoc.Worksheets.item(7)

And going to the end

How can i achive this? Code below

$sExcelFile="C:\WindowsPatching_Reference_2016.xlsx"
$sCSVFile="C:\temp\$wksname.csv"
#$SheetName = "WinA-Test_Thu"

# Get COM Object
$oExcel = New-Object -ComObject "Excel.Application"
# Should Excel be visible?
$oExcel.Visible = $true
# and open excel file
$oExcelDoc = $oExcel.Workbooks.Open($sExcelFile)

# Open Worksheet
#$oWorkSheet = $oExcelDoc.sheets.item($SheetName)
$oWorksheet = $oExcelDoc.Worksheets.item(7)
#Get Sheet Name
$Name = $oworksheet.name
# Activate, show it
$oWorksheet.Activate()

$oExcelDoc.SaveAs($sCSVFile,[Microsoft.Office.Interop.Excel.XlFileFormat]::xlCSVWindows)

$oExcelDoc.Close($false)
Start-Sleep 1
# Cleanup COM
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($oWorksheet)|out-null
$oWorksheet=$null
Start-Sleep 1
# Cleanup COM
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($oExcelDoc)|out-null
$oExcelDoc=$null
# Close Excel
$oExcel.Quit()
Start-Sleep 1
# Cleanup COM
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($oExcel)|out-null
$oExcel=$null
[GC]::Collect()
[GC]::WaitForPendingFinalizers()

Open in new window

LVL 7
dann47Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rainer JeschorCommented:
Hi,
if your code is currently working, then the following loop should then export all sheets until the end starting with sheet number 7:

for ($counter = 7; $counter -le $oExcelDoc.Worksheets.count; $counter++) {
$oWorksheet = $oExcelDoc.Worksheets.item(7)
#Get Sheet Name
$Name = $oworksheet.name
# Activate, show it
$oWorksheet.Activate()
$sCSVFile="C:\temp\$wksname$counter.csv"
$oExcelDoc.SaveAs($sCSVFile,[Microsoft.Office.Interop.Excel.XlFileFormat]::xlCSVWindows)
}

Open in new window


I just added the counter to the filename, otherwise the file would always be overwritten.

HTH
Rainer
0
dann47Author Commented:
Hi many thanks, where would this sit in line with the code please?
Also, the code used to get the name of the sheet and call it that name.
Also, how can i force it to overwrite the file?

Many thanks
0
Rainer JeschorCommented:
Hi,

the following works on my machine with a sample workbook and it overwrites the existing files:

$sExcelFile="C:\WindowsPatching_Reference_2016.xlsx"
$sCSVFile="C:\temp\$wksname.csv"
#$SheetName = "WinA-Test_Thu"

# Get COM Object
$oExcel = New-Object -ComObject "Excel.Application"
# Hide overwrite warning
$oExcel.DisplayAlerts = $false 

# Should Excel be visible?
$oExcel.Visible = $true
# and open excel file
$oExcelDoc = $oExcel.Workbooks.Open($sExcelFile)

# Open Worksheet
#$oWorkSheet = $oExcelDoc.sheets.item($SheetName)
for ($counter = 7; $counter -le $oExcelDoc.Worksheets.count; $counter++) {
    $oWorksheet = $oExcelDoc.Worksheets.item($counter)
    #Get Sheet Name
    $wksname = $oworksheet.name
    # Activate, show it
    $oWorksheet.Activate()
    $sCSVFile="C:\temp\$wksname.csv"
    $oWorksheet.SaveAs($sCSVFile,[Microsoft.Office.Interop.Excel.XlFileFormat]::xlCSVWindows)
}
$oExcelDoc.Close($false)
Start-Sleep 1
# Cleanup COM
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($oWorksheet)|out-null
$oWorksheet=$null
Start-Sleep 1
# Cleanup COM
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($oExcelDoc)|out-null
$oExcelDoc=$null
# Close Excel
$oExcel.Quit()
Start-Sleep 1
# Cleanup COM
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($oExcel)|out-null
$oExcel=$null
[GC]::Collect()
[GC]::WaitForPendingFinalizers()

Open in new window


HTH
Rainer
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dann47Author Commented:
Fantastic, many thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.