Quantcast
Channel: Outlook IT Pro Discussions forum
Viewing all articles
Browse latest Browse all 33562

Outlook will not fully close ...

$
0
0
I have written the following add-in for myself to run in Outlook 2007. My system is WinXP ... everything was fine until I added the Database processing to this program ...

It accesses a very small & very basic .sdf SQLCE database & appears to work just fine ... except Outlook remains open with a Crossed grey icon in the System Tray ... can anybody tell me why ?!? Is there something I am not closing or releasing ?!?

Imports Microsoft.Office.Interop.Outlook
Imports System.Data.SqlClient
Imports System.Data.SqlServerCe

Public Class ThisAddIn

    Private Sub ThisAddIn_Startup() Handles Me.Startup

    End Sub

    Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown

    End Sub

    Private Sub Application_NewMailEx(EntryIDCollection As String) Handles Application.NewMailEx

        Dim MyConn As SqlCeConnection = New SqlCeConnection("Data Source=j:\Users\Gary\Visual Studio 2010\Projects\My_Rules\My_Rules\My_Rules.sdf")
        Dim MyApp As Outlook.Application = New Outlook.Application
        Dim MyNS As Outlook.NameSpace = MyApp.GetNamespace("MAPI")
        Dim MyInbox As Outlook.MAPIFolder = MyNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
        Dim MyEmails As Integer = MyInbox.Items.Count
        Dim MyEmail As Outlook.MailItem
        Dim MyCount As Integer
        Dim MySubFolder As Outlook.Folder = GetMyFolder("Outlook (Gary)\Kickabout")
        Dim MyDest As String
        Dim MySqlCeCmd As New SqlCeCommand()
        Dim MyRecord As SqlCeDataReader

        MyConn.Open()

        For MyCount = MyEmails To 1 Step -1
            MyEmail = MyInbox.Items(MyCount)
            If MyEmail.SenderEmailAddress = "MrX@btinternet.com" Then
                MySubFolder = GetMyFolder("Outlook (Gary)\Kickabout")
                If MyEmail.Attachments.Count > 0 Then
                    MySubFolder = GetMyFolder("Outlook (Gary)\Kickabout\Attachments")
                End If
                MyEmail.Move(MySubFolder)
            Else
                MySqlCeCmd = New SqlCeCommand("Select * from Emails where EmailAddresses = '" + MyEmail.SenderEmailAddress + "'", MyConn)
                MyRecord = MySqlCeCmd.ExecuteReader()
                While MyRecord.Read
                    MyDest = "" + MyRecord(1) + ""
                    MySubFolder = GetMyFolder(MyDest)
                    MyEmail.Move(MySubFolder)
                End While
            End If
        Next

        MyConn.Close()

    End Sub

    Function GetMyFolder(FolderPath)
        ' folder path needs to be something like 
        ' "Public Folders\All Public Folders\Company\Sales"
        Dim aFolders
        Dim fldr
        Dim i
        Dim objNS
        Dim strFolderPath As String

        On Error Resume Next
        strFolderPath = Replace(FolderPath, "/", "\")
        aFolders = Split(FolderPath, "\")

        'get the Outlook objects
        ' use intrinsic Application object in form script
        objNS = Application.GetNamespace("MAPI")

        'set the root folder
        fldr = objNS.Folders(aFolders(0))

        'loop through the array to get the subfolder
        'loop is skipped when there is only one element in the array
        For i = 1 To UBound(aFolders)
            fldr = fldr.Folders(aFolders(i))
            'check for errors
            'If Err() <> 0 Then Exit Function
        Next
        GetMyFolder = fldr

        ' dereference objects
        objNS = Nothing
    End Function

End Class 



Viewing all articles
Browse latest Browse all 33562

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>