VB.net Outlook2000 Addin hang on close

bcyde

New member
Joined
Mar 31, 2003
Messages
1
Hi, I have this VB.net Outlook 2000 Addin which imports addresses into a specific folder in the Contacts folder when clicking my button and so far everything seems to work ok, except for the fact that when I close Outlook it seems to hang at the window where it says "Please wait while Outlook exits." Do I need to be setting more of the Outlook objects = Nothing at the end of my event handler? I havent done VB in a very long time and I guess Im not sure which objects need to be deliberately freed.

Heres pretty much all the code from my connect.vb, any help would be greatly appreciated:
(note, I commented out most of the bottom code to isolate where the "hanging" starts and it appears that once I uncomment the line
OuContactFolder = OuNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts) the hang occurs).

Code:
Imports Microsoft.Office.Core
Imports Extensibility
Imports System.Runtime.InteropServices

#Region " Read me for Add-in installation and setup information. "
 When run, the Add-in wizard prepared the registry for the Add-in.
 At a later time, if the Add-in becomes unavailable for reasons such as:
   1) You moved this project to a computer other than which is was originally created on.
   2) You chose Yes when presented with a message asking if you wish to remove the Add-in.
   3) Registry corruption.
 you will need to re-register the Add-in by building the OutlookAddinSetup project 
 by right clicking the project in the Solution Explorer, then choosing install.
#End Region

<GuidAttribute("8498ABA3-F13D-4D0A-B2FA-64B1FF127E4D" ), ProgIdAttribute("OutlookAddin.Connect" )> _
Public Class Connect
    Implements Extensibility.IDTExtensibility2
    Dim applicationObject As Object
    Dim addInInstance As Object
    Dim OuApp = New Outlook.ApplicationClass()
    Dim WithEvents VPHButton As CommandBarButton

    Public Sub OnBeginShutdown(ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnBeginShutdown
        On Error Resume Next
        OuApp = Nothing
        VPHButton.Delete()
        VPHButton = Nothing
    End Sub

    Public Sub OnAddInsUpdate(ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnAddInsUpdate
    End Sub

    Public Sub OnStartupComplete(ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnStartupComplete
        Dim oCommandBars As CommandBars
        Dim oStandardBar As CommandBar

        On Error Resume Next
         Set up a custom button on the "Standard" command bar.
        oCommandBars = applicationObject.CommandBars
        If oCommandBars Is Nothing Then
            oCommandBars = applicationObject.ActiveExplorer.CommandBars
        End If

        oStandardBar = oCommandBars.Item("Standard" )

         In case the button was not deleted, use the exiting one.
        VPHButton = oStandardBar.Controls.Item("Update VPH Contacts" )
        If VPHButton Is Nothing Then

            VPHButton = oStandardBar.Controls.Add(1)
            With VPHButton
                .Caption = "Update VPH Contacts"
                .Style = MsoButtonStyle.msoButtonCaption
                .Tag = "Update VPH Contacts"
                .OnAction = "!<OutlookAddin.Connect>"
                .Visible = True
            End With
        End If
        oStandardBar = Nothing
        oCommandBars = Nothing

    End Sub

    Public Sub OnDisconnection(ByVal RemoveMode As Extensibility.ext_DisconnectMode, ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnDisconnection
        On Error Resume Next
        If RemoveMode <> Extensibility.ext_DisconnectMode.ext_dm_HostShutdown Then
            Call OnBeginShutdown(custom)
        End If
        applicationObject = Nothing
    End Sub

    Public Sub OnConnection(ByVal application As Object, ByVal connectMode As Extensibility.ext_ConnectMode, ByVal addInInst As Object, ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnConnection
        applicationObject = application
        addInInstance = addInInst
        If (connectMode <> Extensibility.ext_ConnectMode.ext_cm_Startup) Then
            Call OnStartupComplete(custom)
        End If
    End Sub

    Private Sub VPHButton_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) Handles VPHButton.Click
        Dim X As Integer counter
        Dim szInputFile, szWebResult, szTempItem, szCurEvent As String
        Dim arrCSVLines(), arrCSVRow() As String

        internet related vars
        Dim WebClient As New System.Net.WebClient()
        Dim webResponse As IO.Stream

        outlook related item
        Dim OuContact As Outlook.ContactItem
        Dim OuVPHFolder As Outlook.MAPIFolder
        Dim OuContactFolder As Outlook.MAPIFolder
        Dim OuNameSpace As Outlook.NameSpace

        try and retrieve our csv file from the intranet
        szInputFile = "http://devel/PhpFile/PhoneList/tmp/contacts.csv"

        Try
            webResponse = WebClient.OpenRead(szInputFile)
            Dim myReader As New IO.StreamReader(webResponse)

            read our csv file close our TCP connections and start processing
            szWebResult = myReader.ReadToEnd

            close our open objects
            myReader.Close()
            webResponse.Close()

            free our objects
            myReader = Nothing
            webResponse = Nothing
            WebClient = Nothing
        Catch fileexc As System.Net.WebException
            MsgBox("There was an error retrieving the import file. This may be due to network problems please try again later." )
            OuContact = Nothing
            OuVPHFolder = Nothing
            OuContactFolder = Nothing
            OuNameSpace = Nothing
            Return
        Catch ex As Exception
            MsgBox(ex.ToString)
            OuContact = Nothing
            OuVPHFolder = Nothing
            OuContactFolder = Nothing
            OuNameSpace = Nothing
            Return
        End Try
        make sure we read in some info and if so then do all our Outlook processing
        If (szWebResult.Length > 0) Then            
            initialize our outlook items and prepare for insertion of csv items
            OuNameSpace = OuApp.GetNamespace("MAPI" )
            OuContactFolder = OuNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts)

            check if VPH folder exists if not create it and work as normal
            unfortunately in all Outlook Versions prior to 2002 you cannot change the setting for 
            use folder as addressbook
            Try
                   OuVPHFolder = OuContactFolder.Folders.Item("VPH Internet Mail" )
            Catch ouEx As System.Exception
                    OuVPHFolder = OuContactFolder.Folders.Add("VPH Internet Mail" )
            End Try

            Try
                clear out all the contacts in VPH Internet Mail we can just delete the
                VPH Internet Mail folder because the setting for use as addressbook for
                the folder cannot be reset automatically in Outlook versions before 2002                
                While OuVPHFolder.Items.Count > 0
                OuContact = OuVPHFolder.Items.GetLast
                OuContact.Delete()
                End While
                all outlook items in VPH internet folder are clear, now lets import
                arrCSVLines = szWebResult.Split(vbCrLf)

                For X = 1 To (arrCSVLines.GetLength(0) - 2) -2 because the CSV file has an extra blank line
                arrCSVLines(X) = arrCSVLines(X).Replace("""", "" )
                arrCSVRow = arrCSVLines(X).Split("," )
                OuContact = OuVPHFolder.Items.Add()
                OuContact.FirstName = arrCSVRow(1)
                OuContact.MiddleName = arrCSVRow(2)
                OuContact.LastName = arrCSVRow(3)
                OuContact.Email1Address = arrCSVRow(4)
                OuContact.CompanyName = arrCSVRow(5)
                OuContact.OfficeLocation = arrCSVRow(6)
                OuContact.Department = arrCSVRow(7)
                OuContact.JobTitle = arrCSVRow(8)
                OuContact.BusinessTelephoneNumber = arrCSVRow(9)
                OuContact.Business2TelephoneNumber = arrCSVRow(10)
                OuContact.PagerNumber = arrCSVRow(11)
                OuContact.ManagerName = arrCSVRow(12)
                OuContact.AssistantName = arrCSVRow(13)
                OuContact.Categories = "VPH Internet Mail"
                OuContact.Save()
                Next            
            Catch ex As Exception
                MsgBox("There was an error processing the import file: " + vbCrLf + ex.ToString)
                Return
            End Try
        End If
        clean up outlook objects
        OuContact = Nothing
        OuVPHFolder = Nothing
        OuContactFolder = Nothing
        OuNameSpace = Nothing
        MsgBox("Addressbook update complete." )
    End Sub
End Class
Any help would be greatly appreciated.
Thanks,
-b
 
Back
Top