none
Outlook will not fully close ... RRS feed

  • Question

  • 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 

    Friday, October 19, 2012 4:28 PM

Answers

  • Imports Microsoft.Office.Interop.Outlook
    Imports System.Data.SqlClient
    Imports System.Data.SqlServerCe
    
    Public Class ThisAddIn
    
        Dim WithEvents MyApp As Outlook.Application = Nothing
        Dim MyNS As Outlook.NameSpace = Nothing
        Private Sub ThisAddIn_Startup() Handles Me.Startup
            MyApp = Application
            MyNS = MyApp.GetNamespace("MAPI")
        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 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 strFolderPath As String
    
            On Error Resume Next
            strFolderPath = Replace(FolderPath, "/", "\")
            aFolders = Split(FolderPath, "\")
    
            'set the root folder
            fldr = MyNS.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
    
        End Function
    
    End Class 


    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "vodkasoda" <=?utf-8?B?dm9ka2Fzb2Rh?=> wrote in message news:9f212440-2d18-4577-ba53-380056e7c350...

    Thanks for your answer Ken, but unfortunately I don't understand this stuff too well, I am migrating from VBA so that's probably why I have old/wrong code in there !!! 

    Do you mean that I should have all this code in the ThisAddIn_Startup routine ? If so, how do I then make it re-execute every time there is new mail ?

    Also, what do I use instead of the following lines  ?

    Dim MyApp As Outlook.Application = New Outlook.Application
            Dim MyNS As Outlook.NameSpace = MyApp.GetNamespace("MAPI")

    I have spent so long on this and I thought I had it cracked today :-( !!!!!!


    Ken Slovak MVP - Outlook
    • Marked as answer by vodkasoda Friday, October 26, 2012 2:24 PM
    Friday, October 19, 2012 7:00 PM

All replies

  • Your NewMailEx() handler is flawed. Never create a new Outlook.Application object in an Outlook COM addin. It's not trusted and will create exactly the problem you describe. Use the Application object passed to you in the startup handler and use that to derive all other Outlook objects.
     
    Dim MyApp As Outlook.Application = New Outlook.Application ' = BAD

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "vodkasoda" <=?utf-8?B?dm9ka2Fzb2Rh?=> wrote in message news:69d0b032-09a3-4335-9087-7283fa8662ab...

    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 


    Ken Slovak MVP - Outlook
    Friday, October 19, 2012 4:46 PM
  • Thanks for your answer Ken, but unfortunately I don't understand this stuff too well, I am migrating from VBA so that's probably why I have old/wrong code in there !!! 

    Do you mean that I should have all this code in the ThisAddIn_Startup routine ? If so, how do I then make it re-execute every time there is new mail ?

    Also, what do I use instead of the following lines  ?

    Dim MyApp As Outlook.Application = New Outlook.Application
            Dim MyNS As Outlook.NameSpace = MyApp.GetNamespace("MAPI")

    I have spent so long on this and I thought I had it cracked today :-( !!!!!!

    Friday, October 19, 2012 6:51 PM
  • Imports Microsoft.Office.Interop.Outlook
    Imports System.Data.SqlClient
    Imports System.Data.SqlServerCe
    
    Public Class ThisAddIn
    
        Dim WithEvents MyApp As Outlook.Application = Nothing
        Dim MyNS As Outlook.NameSpace = Nothing
        Private Sub ThisAddIn_Startup() Handles Me.Startup
            MyApp = Application
            MyNS = MyApp.GetNamespace("MAPI")
        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 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 strFolderPath As String
    
            On Error Resume Next
            strFolderPath = Replace(FolderPath, "/", "\")
            aFolders = Split(FolderPath, "\")
    
            'set the root folder
            fldr = MyNS.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
    
        End Function
    
    End Class 


    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "vodkasoda" <=?utf-8?B?dm9ka2Fzb2Rh?=> wrote in message news:9f212440-2d18-4577-ba53-380056e7c350...

    Thanks for your answer Ken, but unfortunately I don't understand this stuff too well, I am migrating from VBA so that's probably why I have old/wrong code in there !!! 

    Do you mean that I should have all this code in the ThisAddIn_Startup routine ? If so, how do I then make it re-execute every time there is new mail ?

    Also, what do I use instead of the following lines  ?

    Dim MyApp As Outlook.Application = New Outlook.Application
            Dim MyNS As Outlook.NameSpace = MyApp.GetNamespace("MAPI")

    I have spent so long on this and I thought I had it cracked today :-( !!!!!!


    Ken Slovak MVP - Outlook
    • Marked as answer by vodkasoda Friday, October 26, 2012 2:24 PM
    Friday, October 19, 2012 7:00 PM
  • I'll have a look at this then Ken, thanks, sorry it's taken a couple of days, I couldn't get into my account for some reason !!!

    Wednesday, October 24, 2012 3:06 PM
  • Well, it works just fine, I can't say that I understand why, but it does, thank you Ken, very, very much !!!

    Friday, October 26, 2012 2:24 PM