Powershell function to create an excel spreadsheet multiple sheets - and ApproveIt on workstation

Posted on 2009-02-12
Last Modified: 2012-05-06
The attached code is a function I am calling.  I can call the function but something won't let me create the file.  It may be approveit related.

Error: You cannot call a method on a null-valued expression.
Exception calling "Add" with "0" argument(s): "The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT))"
At :line:12 char:28
+ $Book = $Excel.Workbooks.Add <<<< ()

Anybody got any tips?  If I can't create the file, can anybody provide some assistance how to call a prefabbed excel file with multiple worksheets?
#----------------Function NEWEXCEL------------

Function NewExcel ($users2, $dir2)


$Excel = New-Object -Com Excel.Application

#$Excel.visible = $True

$Book = $Excel.Workbooks.Add()

$Sheet = $Book.WorkSheets.Item(1)

$Sheet.Cells.Item(1,1) = "User Name"

$Sheet.Cells.Item(1,2) = "Size (GB)"

$WorkBook = $Sheet.UsedRange

$WorkBook.Font.Bold = $True

$ = "$dir2"

$intRow = 2

foreach ($user in $users2) {

$sheet.Cells.Item($intRow,1) = $user.MailboxDisplayName

$sheet.Cells.Item($intRow,2) = "{0,7:n2}" -f ($user.Size / 1MB)








Open in new window

Question by:kjanicke
    LVL 18

    Expert Comment

    It fails WAY before the file create.

    I would do it line by line in Powershell to know exactly what fails.
    LVL 18

    Accepted Solution

    Nope.  It fails at the file create.  This is not the whole script.

    It is approveit.  Rather than fiddling with the settings, I just removed ApproveIt.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
    I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
    This video discusses moving either the default database or any database to a new volume.
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now