Powershell script to open excel object (workbook)
In my previous posting, I used excel comobject extensively but mainly limited to create new excel object and write report to it.
Here is a little script to open an existing excel workbook.
$excel = new-object -comobject Excel.Application
$excel.visible = $True
# Inputbox
$x = new-object -comobject MSScriptControl.ScriptControl
$x.language = "vbscript"
$x.addcode("function getInput() getInput = inputbox(`"Enter the full path of your excel file`",`"Workbook Name`") end function" )
$excelfilename = $x.eval("getInput")
function global:Open-Excel()
{
param ($excel, [string]$excelfilename)
if (!$(test-path $excelfilename))
{
write-host "File doesn't exist..."
return $null
}
$excelfile = $excel.Workbooks.Open($excelfilename)
}
Open-Excel $excel $excelfilename