Hi,
Although the below is a Powershell script, my question is more about the Excel class model, finding the right method to call to get my desired results. Thus, I'm posting here. Please (gently) let me know if I should post elsewhere.
The below script is used to shrink an Excel file created in Excel XML (via SAS). The XML file is hugely bloated, and running Excel "in batch mode" as a COM object is very useful in shrinking this file. In the process, I'm also autofitting the data to the widest length in each column.
My desired result is to return to Sheet 1, cell A1, with all cells unselected. However, I can't seem to find the right search string in Google to find the correct Excel method to call.
Here is the Powershell script. Any suggestions are welcome!
Regards,
Scott
Although the below is a Powershell script, my question is more about the Excel class model, finding the right method to call to get my desired results. Thus, I'm posting here. Please (gently) let me know if I should post elsewhere.
The below script is used to shrink an Excel file created in Excel XML (via SAS). The XML file is hugely bloated, and running Excel "in batch mode" as a COM object is very useful in shrinking this file. In the process, I'm also autofitting the data to the widest length in each column.
My desired result is to return to Sheet 1, cell A1, with all cells unselected. However, I can't seem to find the right search string in Google to find the correct Excel method to call.
Here is the Powershell script. Any suggestions are welcome!
Regards,
Scott
Code:
[CmdletBinding(SupportsShouldProcess=$true)]
param(
# full path to the original Excel file (usually in XML format)
[Parameter(
Position=0,
Mandatory=$true
)]
[Alias("Path")]
[ValidateScript({Test-Path $_})]
[System.IO.FileInfo]$xlFile
,
# ouptput format
[Parameter(
Position=1
)]
[Alias("Format")]
[ValidateSet("xlsx","xlsm","xlsb","xls")]
[String]$xlFormat="xlsb"
)
# See http://technet.microsoft.com/en-us/library/ff730962.aspx
# or Google "ReleaseComObject Excel"
function release-comobject($ref) {
while ([System.Runtime.InteropServices.Marshal]::ReleaseComObject($ref) -gt 0) {}
[System.GC]::Collect()
}
$ErrorActionPreference="Continue"
$xlPath=$xlFile.DirectoryName
$xlBasename=$xlFile.Basename
switch ($xlFormat) {
"xlsx" {$xlFormatNum=51; break}
"xlsm" {$xlFormatNum=52; break}
"xlsb" {$xlFormatNum=50; break}
"xls" {$xlFormatNum=56; break}
}
try {
$Excel = New-Object -ComObject excel.application
$Excel.visible = $False
$Excel.displayalerts = $False
$Workbook = $Excel.Workbooks.Open($xlFile)
#=============================================================================#
# autofit row/column #
# before we save the file, call AutoFit to improve the appearance #
#=============================================================================#
foreach ($Worksheet in $Workbook.Worksheets) {
$range = $Worksheet.UsedRange
$rc = $range.EntireColumn.AutoFit()
$rc = $range.Rows.AutoFit()
release-comobject $range
$rc = $Worksheet.Activate
$range = $Worksheet.Range("A1")
$rc = $range.Select
release-comobject $range
release-comobject $Worksheet
}
$Workbook.Sheets.Item(1).Activate()
$Workbook.SaveAs((Join-Path $xlPath "$xlBasename.$xlFormat"), $xlFormatNum)
$Excel.quit()
}
catch {
Write-Error $_
}
finally {
# See http://technet.microsoft.com/en-us/library/ff730962.aspx
release-comobject $Workbook
release-comobject $Excel
}