Ying Li at myITforum.com

PowerShell & System Center

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

Posted: May 16 2007, 11:06 AM by yli628 | with no comments
Filed under:

Comments

No Comments