关闭Outlook后,“扫描收件箱"将杀死我的应用程序. [英] Scanning Inbox kills my app when Outlook is closed.
问题描述
我希望阅读完人们在此处发布的所有奇妙代码后,有人可以帮助我简化我的VB.Net应用程序.
我的应用程序扫描Outlook收件箱以查找特定的主题行.如果Outlook在扫描收件箱时处于打开状态,而我关闭Outlook,则会崩溃.这是我的代码:
I was hoping that after reading the about all the amazing code people have posted here, that someone can help me with my simplistic VB.Net app.
My app scans the Outlook Inbox looking for specific subject lines. If Outlook is open while it is scanning the Inbox, and I close Outlook, it crashes. Here''s my code:
Public Sub ProcessInbox()
Dim LineTrace As Decimal = 0
Dim oOutlook As New Microsoft.Office.Interop.Outlook.Application
LineTrace = 1
Dim oNs As Microsoft.Office.Interop.Outlook.NameSpace
LineTrace = 2
Dim oFldr As Microsoft.Office.Interop.Outlook.MAPIFolder
LineTrace = 3
Dim oAttachments As Microsoft.Office.Interop.Outlook.Attachments
LineTrace = 4
Dim oAttachment As Microsoft.Office.Interop.Outlook.Attachment
LineTrace = 5
Dim oMessage As Object
Try
Dim iMsgCount As Integer
Dim iCtr, iAttachCnt As Short
LineTrace = 8
Dim sSubjectLineToFind As String = "ReAsure_HealthNode_"
LineTrace = 9
Dim DataIncomingFolder As String = System.Environment.GetFolderPath(Environment.SpecialFolder.Personal) & _
IncommingDataFolder
LineTrace = 10
mBusy = True ' Indicate that the scan is active so that
' ' no other scans are also initiated.
'get reference to inbox
oNs = oOutlook.GetNamespace("MAPI")
LineTrace = 10.5
oFldr = oNs.GetDefaultFolder(6)
LineTrace = 10.8
Dim TotalOutlookMessages As Integer = oFldr.Items.Count
RaiseEvent MsgCount(TotalOutlookMessages)
LineTrace = 11
'System.Diagnostics.Debug.WriteLine("Start- Enumerating all Inbox messages:")
'For Each oMessage In oFldr.Items
' Console.WriteLine(oMessage.Subject)
'Next oMessage
'System.Diagnostics.Debug.WriteLine("End- Enumerating all Inbox messages:")
For vIndex As Integer = TotalOutlookMessages To 1 Step -1
Dim DeleteEmailMessageOK As Boolean = False
oMessage = oFldr.Items(vIndex)
RaiseEvent NewMsg(oMessage)
With oMessage
'basic info about message
'System.Diagnostics.Debug.WriteLine(.To)
'System.Diagnostics.Debug.WriteLine(.CC)
'Console.WriteLine("Current message subject is: " & .Subject)
'System.Diagnostics.Debug.WriteLine(.Body)
'If .UnRead Then
' 'System.Diagnostics.Debug.WriteLine("Message has not been read")
'Else
' 'System.Diagnostics.Debug.WriteLine("Message has been read")
'End If
iMsgCount = iMsgCount + 1
' oMessage = Nothing
LineTrace = 12
' Look for specific subject lines in emails.
' Console.WriteLine(oMessage.Subject.IndexOf(sSubjectLineToFind))
If oMessage.Subject.IndexOf(sSubjectLineToFind) = 0 Then
' Found in this email!
LineTrace = 13
DeleteEmailMessageOK = True
With oMessage.Attachments
' Any attachments?
LineTrace = 14
iAttachCnt = .Count
If iAttachCnt > 0 Then
' Attachment(s) found.
' Cycle thru each one.
LineTrace = 15
For iCtr = 1 To iAttachCnt
' Save this attachment.
LineTrace = 16
.Item(iCtr).SaveAsFile(DataIncomingFolder & .Item(iCtr).FileName)
' Check that the file was successfully saved.
' If OK, flag to delete the parent email.
DeleteEmailMessageOK = DeleteEmailMessageOK And CheckFileOK(DataIncomingFolder & .Item(iCtr).FileName)
' Record the details of the attachment.
RaiseEvent NHDS_Data_Msg_Found(oMessage, oMessage.Attachments.Item(iCtr))
Next iCtr
Else
' Found an email but no attachments.
' Record these details.
LineTrace = 17
RaiseEvent NHDS_Data_Msg_Found(oMessage, Nothing)
End If
End With
End If
End With
LineTrace = 18
System.Windows.Forms.Application.DoEvents()
' Check that the file was successfully saved.
' If OK, delete the parent email.
If DeleteEmailMessageOK Then
LineTrace = 19
Dim vDeletedMessageSubject As String = oMessage.subject
oMessage.Delete()
Console.WriteLine("Message with subject: {0} deleted.", vDeletedMessageSubject)
End If
Next vIndex
Catch ex As System.Runtime.InteropServices.COMException
' Disregard this error. It occurs when Outlook is closed while the TN is scanning the Inbox.
Catch ex As Exception
Dim s As String = "Error [" & Err.Number & "] occured in the ProcessInbox module. Failure occured at step = " & LineTrace & vbNewLine & _
"Error Desc: " & Err.Description
Dim t As String = "Outlook Not Ready"
Call frmScanInbox.SetTrayNotifyIconBalloonText(5000, s, t, ToolTipIcon.Warning)
Console.WriteLine(s)
mBusy = False
Finally
LineTrace = 20
mBusy = False
oOutlook.Quit()
Marshal.ReleaseComObject(oOutlook)
GC.Collect()
oAttachment = Nothing
oAttachments = Nothing
oMessage = Nothing
oFldr = Nothing
oNs = Nothing
oOutlook = Nothing
End Try
End Sub
为了复制该问题,我运行了扫描Outlook收件箱的应用程序.如果我手动打开Outlook,一切正常.如果我的应用程序正在扫描收件箱,并且我手动关闭了之前打开的Outlook,则Outlook从任务管理器中消失.
这似乎导致我的应用崩溃,但它没有显示错误,只是挂起.它还会将Outlook的运行实例留在任务管理器中.
如果我从任务管理器中杀死Outlook实例,我的应用程序突然继续运行,好像什么都没发生.
有人可以建议为什么发生这种情况以及如何解决它.我正处在没有更多想法可以尝试的时候.谢谢.
哦,忘了提.一旦我杀死了剩下的Outlook任务,在我的代码中执行的下一行就是:
To replicate the issue, I run my app which scans the Outlook Inbox. If I open Outlook manually, everything is OK. If my app is in the middle of scanning the inbox, and I manually close Outlook which I opened earlier, Outlook disappears from the Task Manager.
This seems to cause my app to crash, but it does not display an error, it just hangs. It also leaves a running instance of Outlook in the Task Manager.
If I kill the instance of Outlook from the Task Manger, my app suddenly continues running as if nothing has happened.
Can someone suggest why this is happening and how to fix it. I am at the point where I have no more ideas to try. Thank you.
Oh, forgot to mention. Once I kill the remaining Outlook task, the next line that is executed in my code is:
Catch ex As System.Runtime.InteropServices.COMException
推荐答案
尝试处理Microsoft.Office.Interop.Outlook.Application上的Quit事件
查看活动部分
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.application_members.aspx [ ^ ]
Try handling the Quit event on Microsoft.Office.Interop.Outlook.Application
see events section
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.application_members.aspx[^]
有了这些新信息,我是否可以:
1.检测用户何时关闭Outlook
2.冻结Outlook的关闭操作,直到我的代码完成扫描为止.
3.如果用户在上面的步骤1中关闭了Outlook,请立即将其关闭.
考虑到我上面的代码,甚至要求示例代码来执行此操作是否会太多?在使用Outlook自动化之前,我从未做过此类工作,这非常令人困惑.谢谢.
PS.据我了解,退出事件在Outlook关闭周期中可能为时已晚.我读到我可能必须使用Close事件.谢谢您的帮助.
With this new information, is it possible for me to:
1. Detect when the user closes Outlook
2. Freeze the closing action of Outlook until my code finishes its scan.
3. If the user closed Outlook in step 1 above, close it now.
Would it be too much to ask for even sample code on how to do this bearing in mind my code above? I''ve never done this sort of work before with Outlook automation and it''s very confusing. Thanks.
PS. From what I read, the Quit event might be too late in the Outlook closing cycle. I read that I may have to use the Close event. I would appreciate your help with this.
对不起,但是有人知道如何从VB.Net应用程序检测Outlook关闭吗?示例代码将不胜感激.谢谢.
Sorry, but does anyone know how to detect Outlook closing from a VB.Net app? Sample code would be appreciated. Thanks.
这篇关于关闭Outlook后,“扫描收件箱"将杀死我的应用程序.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!