下面的脚本执行以下操作 - 创建一个新的电子邮件,打开一个Excel文件,从复制的内容,粘贴到新邮件并发送。
我有两台PC上的脚本进行了测试。该脚本是使用PowerShell 2.0 64位ISE在PC1上开发的。 PC1- Windows 7 64位SP1,Office 2010 32位和PowerShell 2.0。
PC2- Windows 7 64位SP1,Office 2010 32位和PowerShell 3.0。PowerShell脚本来发送电子邮件崩溃的Outlook 2010
我已经在两台机器上使用PowerShell ISE运行脚本。这两台机器的主要区别在于PowerShell版本。
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("[email protected]")
$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
一个一致的问题是 - 由powershell启动的Excel.exe进程永远不会在脚本运行完成后退出。我必须通过任务管理器手动完成任务。 (我打算单独发布问题以获得解决方案。)
但是,主要的问题是Outlook 2010的一致性崩溃,但它崩溃的点是不一致的。
脚本是通过ISE 5-6倍PC1上运行(使用PowerShell 2)和各试验后而产生的电子邮件。但是,在最后一次成功的测试运行后的几分钟后,我在Outlook 2010中收到了“无响应”对话框,它让我有选择重新启动它。重新启动后,Outlook再次冻结,然后我不得不杀死该进程并重新启动。
在使用PowerShell 3(通过ISE)每次我跑的脚本的Outlook冻结PC2。它似乎总是崩溃粘贴操作。在PC2上使用$newmail.GetInspector.WordEditor.Range().Paste()
。但是,在成功发送电子邮件几分钟后,Outlook再次冻结。
正如你所看到的 - 存在时,Outlook将崩溃没有一致性/冻结......有时它同时粘贴,有时想有时发送电子邮件后,发送或保存电子邮件时是!
有趣。到目前为止,我一直在使用stop-process来终止脚本末尾的运行excel进程。我会尝试清除剪贴板,看看是否调用'$ excel.Quit(); [System.Runtime.Interopservices.Marshal] :: ReleaseComObject($ excel)'现在将关闭该进程。 – Ron
我清除了剪贴板,但是脚本结束后,excel进程仍在运行。我仍然需要继续使用'Stop-Process'来结束正在运行的实例。 – Ron