Link to home
Start Free TrialLog in
Avatar of kjanicke
kjanickeFlag for United States of America

asked on

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

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
$sheet.name = "$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)
$Introw++
}
 
$WorkBook.EntireColumn.AutoFit()
$sheet.SaveAs("c:\$date\$date-$dir2")
$excel.quit()
}
#-----------------------------------------------

Open in new window

Avatar of BSonPosh
BSonPosh
Flag of United States of America image

It fails WAY before the file create.

I would do it line by line in Powershell to know exactly what fails.
ASKER CERTIFIED SOLUTION
Avatar of kjanicke
kjanicke
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial