Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Outlook Automation- Need to open an independent Outlook process.

Status
Not open for further replies.

JDMils

Programmer
Mar 26, 2003
15
AU
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
|
 
I can't answer your direct question, but are you using an Exchange environment there? If so you could go directly to the exchange server and not worry about Outlook. This is the easiest way I've found to work with Exchange:


I looked at the Outlook command-line switches but didn't see anything to hide/lock the window...

Regards,

J
 
Unfortunately not. I'm surprised that other programmers have not met the same fate as me when programming Outlook.

It seems that the error is occuring within the Framework as eventually, my Try...Next gets control of the code flow, but that's only once I kill the Outlook process that's left in memory.

|
+-- Julian
|
 
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.

|
+-- Julian
|
 
Not going through Outlook is the best thing. Second, would be for your process to run its own session all the time no matter if the user already has one open. Third, have the process delay its action until outlook has been closed. Forth, check if the session has been closed then open its own. Finally to add some type of plug-in to outlook that prevented it from closing when the app is scanning.

Can you possibly stop Windows from closing outlook? Maybe. Do you want to? No. Really everything but the first is just an increasingly bad design idea as you go down the list.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top