Need to open an independent Outlook process.
I have written a VB Dot.Net app which scans the Outlook Inbox. But I am getting this error:
A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll
Showing balloon: t=[Outlook Not Ready], s=[Error [462] occured in the ProcessInbox module. Failure occured at step = 18
Error Desc: The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)]
My Dot.Net app runs in the system tray, opening Outlook, scanning the Inbox, then closing it, and it does this all day. If the user Opens Outlook on the same machine, my app uses that Outlook session to do its scan, another Outlook process is NOT opened (Task Manager shows this).
Now, if the user closes Outlook while the app is looping thru the Inbox items, as soon as Outlook closes, the references (object variables) in the app referencing the Outlook session suddenly disappears, and thus the RPC error occurs.
I need a way for my app to open Outlook independent of the user. It needs to be invisible as well. Thus, if Outlook is opened by the user, my app should open ANOTHER process of Outlook to scan the Inbox then close THAT process only.
At the moment, it seems that Outlook processes opened by the user are also referenced by the app- which is what I do NOT want. Thanks.
Win7 & Outlook 2007 in non-domain environment.
My code:
|
+-- Julian
|
I have written a VB Dot.Net app which scans the Outlook Inbox. But I am getting this error:
A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll
Showing balloon: t=[Outlook Not Ready], s=[Error [462] occured in the ProcessInbox module. Failure occured at step = 18
Error Desc: The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)]
My Dot.Net app runs in the system tray, opening Outlook, scanning the Inbox, then closing it, and it does this all day. If the user Opens Outlook on the same machine, my app uses that Outlook session to do its scan, another Outlook process is NOT opened (Task Manager shows this).
Now, if the user closes Outlook while the app is looping thru the Inbox items, as soon as Outlook closes, the references (object variables) in the app referencing the Outlook session suddenly disappears, and thus the RPC error occurs.
I need a way for my app to open Outlook independent of the user. It needs to be invisible as well. Thus, if Outlook is opened by the user, my app should open ANOTHER process of Outlook to scan the Inbox then close THAT process only.
At the moment, it seems that Outlook processes opened by the user are also referenced by the app- which is what I do NOT want. Thanks.
Win7 & Outlook 2007 in non-domain environment.
My code:
Code:
Public Sub ProcessInbox()
Dim LineTrace As Decimal = 0
Try
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 iMsgCount As Integer
LineTrace = 6
Dim oMessage As Object
LineTrace = 7
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
LineTrace = 20
mBusy = False
oAttachment = Nothing
oAttachments = Nothing
oMessage = Nothing
oFldr = Nothing
oNs = Nothing
oOutlook = Nothing
Catch ex As Exception
'sMsg = "An error occured in the ProcessInbox module. Failure occured at step = " & LineTrace & vbNewLine & vbNewLine & _
' "Error #" & Err.Number & vbNewLine & vbNewLine & _
' "Error Desc: " & Err.Description
MessageBox.Show(sMsg, ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error)
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)
mBusy = False
End Try
End Sub
|
+-- Julian
|