How to add missing module to manipulate Excel 2013 from PowerShell?

Hi! I'm trying to run a PowerShell script on Win 7 x64 as a scheduled task to open and refresh an Excel 2013 (x32) file's data sources, then save it. I get the following error:

PS E:\> e:\RefreshExcelFiles.ps1 e:\Test.xlsx
The term 'e:\RefreshExcelFiles.ps1' is not recognized as the name of a cmdlet, function, script file, or operable progr
am. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:25
+ e:\RefreshExcelFiles.ps1 <<<<  e:\Test.xlsx
    + CategoryInfo          : ObjectNotFound: (e:\RefreshExcelFiles.ps1:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

I assume this means I have to import some module, but which one, and where do I find it?

This is the script code:

try
{
    # Creating the excel COM Object 
    $xl = New-Object -ComObject Excel.Application; 

    # Setting up Excel to run without UI and without alerts
    $xl.DisplayAlerts = $false; 
    $xl.Visible = $false; 
    }
Catch
{
    Write-EventLog -EventId "5001" -LogName "Application" -Message "Failed to start Excel" -Source "Application"
    Exit
}

foreach ($i in $args)
{

    write-host "handling $i"
    try
    {
        # Allow update only if we can perform check out
        If ($xl.workbooks.CanCheckOut($i))
        {

            # Opening the workbook, can be local path or SharePoint URL
            $wb = $xl.workbooks.open($i);

            # Perform the check out
            $xl.workbooks.checkout($i)

            # Calling the refresh
            $wb.RefreshAll();

            # Saving and closing the workbook
            $wb.CheckInWithVersion();

            # in case you are not using checkout/checkin perform a save and close
            #$wb.Save();
            #$wb.Close();

            #Release Workbook
            [System.Runtime.Interopservices.Marshal]::ReleaseComObject($wb)
        }
        else
        {
            write-host "Check out failed for:  $i"
            Write-EventLog -EventId "5001" -LogName "Application" -Message "Workbook can't be checked out $i" -Source "Application"
        }
    }
    catch
    {
        Write-EventLog -EventId "5001" -LogName "Application" -Message "Failed refreshing the workbook $i $_" -Source "Application"        
    }

}

#Quiting Excel
$xl.quit(); 

#Release Excel
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($xl)

TIA!

Re

Dennis


  • Edited by sgude1 Sunday, December 15, 2013 5:08 AM Typo
December 15th, 2013 8:03am

Hi Dennis,

Running a Script in a Sched Task will require you to correctly set-up the sched task....you can refer the forum here as there have been threads on that where people have given valuable resources.
To get you started this is a quick link:
http://blogs.technet.com/b/heyscriptingguy/archive/2012/08/11/weekend-scripter-use-the-windows-task-scheduler-to-run-a-windows-powershell-script.aspx

From the error posted above ...I think it couldn't find the script by the name RefreshExcelFiles.ps1
Please verify that the file exists in the location E:\

Hope it helps

Free Windows Admin Tool Kit Click here and download it now
December 15th, 2013 8:55am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics