-1

The script below does the following - create a new email, open an excel file, copy content from it, paste into the new email and send it.
I have two PCs on which script was tested. The script was developed on PC1 using PowerShell 2.0 64 bit ISE. PC1- Windows 7 64 bit with SP1, Office 2010 32 bit and PowerShell 2.0.
PC2- Windows 7 64 bit with SP1, Office 2010 32 bit and PowerShell 3.0.

I have run the script on both machines using PowerShell ISE. The main difference between the two machines is the PowerShell version.

Add-type -assembly "Microsoft.Office.Interop.Outlook" | out-null 
$olFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]  
$outlook = new-object -comobject Outlook.Application 
$namespace = $outlook.GetNameSpace("MAPI") 
$InboxFolder = $namespace.getDefaultFolder($olFolders::olFolderInBox) 
$InboxItems = $InboxFolder.items

$newmail = $outlook.CreateItem(0)
$newmail.Display()

$newmail.Recipients.Add("user@domain.com")  
$newMail.Subject = "Report"  

$excel = New-Object -comobject Excel.Application
$FilePath = "D:\Report.xlsx"
$ReportWorkBook = $excel.Workbooks.Open($FilePath)
$excel.Visible = $true
$ws = $ReportWorkBook.Worksheets.Item(1)
$SelectedRange = $ws.UsedRange
$SelectedRange.Copy()

# PasteExcelTable works on PC1 but Fails on PC2 consistently (with PowerShell 3.0)
#$newmail.HTMLBody +=$newmail.GetInspector.WordEditor.Range().PasteExcelTable($false,$false,$true)
# Paste() worked on both PCs
$newmail.GetInspector.WordEditor.Range().Paste()

$newMail.Send()

$excel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)
Remove-Variable excel
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($outlook)
Remove-Variable outlook

One consistent problem is - the Excel.exe process started by powershell never exits after script has finished running. I have to kill the task manually via task manager. (I am planning to post a separate question to get a solution to this.)

The main problem however is the consistent crashing of Outlook 2010 but the point at which it crashes is inconsistent .

The script was run on PC1 (with PowerShell 2) via the ISE 5-6 times and each test generated an email. However a few mins after the last successful test run i got a "Not responding" dialog box in Outlook 2010 and it gave me the option to restart it. After restarting Outlook is froze again and then i had to kill the process and start it again.

On PC2 with PowerShell 3 (via the ISE) each time i ran the script Outlook froze. It seemed to always crash the paste operation. Using $newmail.GetInspector.WordEditor.Range().Paste() worked on PC2. However a couple of minutes after the email was sent successfully, Outlook froze again.

As you can see - there is no consistency in when Outlook crashes/freezes...sometimes it is while pasting, sometimes when trying to send or save email and sometimes after sending an email!

Ron
  • 49
  • 1
  • 9

2 Answers2

0

It seems the cause of the crashes was the use of 64 bit ISE instead of the 32 bit ISE. I have been running the script with 32 bit ISE for a couple of days now and so far Outlook has not crashed.

I have almost always used 64 bit ISE even for Excel and Word but I have not seen those apps crash. The strange part is the intermittent successes and then the crashes and errors. The script should have ideally always failed when running with 64 bit ISE.
It had nothing to do with running script in PowerShell v2 or v3. (In my initial tests I did try using 32 bit ISE once, however outlook still crashed once but that was because it was already in an unstable state with the several tests done.) In short, it is better to use 32 bit ISE with 32 bit applications.

Ron
  • 49
  • 1
  • 9
0

regarding Excel not closing... If there are more than 101 cells in the clipboard, Excel will prompt you whether you want to keep the contents of the clipboard in memory. Before closing Excel, purge the clipboard using the command below:

[System.Windows.Forms.Clipboard]::Clear()
  • Interesting. So far i have been using stop-process to kill the running excel process at the end of the script. I will try clearing the clipboard and see if calling `$excel.Quit(); [System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)` will now close the process. – Ron Jun 23 '14 at 04:32
  • I cleared the clipboard however the excel process was still running after the script ended. I still have to continue using `Stop-Process` to end the running instance. – Ron Jun 24 '14 at 11:09