0

is it possible to have Powershell open a .HTM and save as an .XLS. My HTM file is a table that I would like the formatting carried though to which rules out a CSV. I have tried various methods using save as however it just saves the file with the .XLS extension whilst still being an .HTM file. I believe I just need the middle part of the script below filled in with the Save As code. Any assistance would be greatly appreciated.

$Excel = New-Object -comobject Excel.Application
$FilePath = "C:\ScriptRepository\Results\Test.htm"
$Workbook = $Excel.Workbooks.Open($FilePath)
$Excel.Visible = $true
$Excel.DisplayAlerts = $False

$Excel.Workbooks.Close()
$Excel.Quit()
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($WorkBook)
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Excel)
Remove-Variable -Name excel
Slaine MacRoth
  • 41
  • 1
  • 2
  • 10

1 Answers1

2

When saving, pass the output file path and the enum 51, which tells Excel to use the xlsx filetype.

Check out the SaveAs documentation page, and XlFileFormat enumerations page for more info.

$OutFile     = "c:\mypath\to\my.xlsx"
$xlSLSXType  = 51
$workBook.SaveAs("$OutFile",$xlSLSXType)
G42
  • 9,791
  • 2
  • 19
  • 34
  • Thankyou kindly gms0ulman, thats done the trick. I had to remove $Excel.Visible = $true for it to work however Excel was not required to be visible. Thankyou once again. – Slaine MacRoth May 30 '17 at 08:50