none
getting information from outlook contacts and showing on a form - couple of issues/questions? RRS feed

  • Question

  • Hi

    I have the following code (with the help of forum question here = http://social.msdn.microsoft.com/Forums/office/en-US/af712985-9f13-4cb5-8940-2cbfa4f98f6b/accessing-outlook-2013-contacts-from-within-a-visual-basic-2013-application?forum=outlookdev)

    The code is to get list of all my contacts in outlook, show the contact "file as" name in the list box and then when I select an entry in list box it then shows me other information (from outlook user defined fields) in text boxes below

    I have most bits working, but am unsure of following:-

    1) How can I sort the contents of the binding source so it shows the contacts in alphabetical order in the listbox? tried sorting listbox but this then ends up mucking up binding to the text boxes - i.e. wrong info shown for wrong contact - so need to sort it before this point?

    2) Some of my fields contains dates, others contain no date, so shows None in outlook - but when showing in form it is showing 01/01/4501 - how can I stop this, so it shows None when outlook has None showing?

    3) Code has a separate class file (as suggested in forum above) -  does it have to be in a separate file, or can I modify the code somehow so it is all in one - or is there some reason/benefit for doing it like this?

    Am I doing this in the best way - any comments would be appreciated, as eventually want to expand on this to find contacts who have a field containing an expiry date and then just show those expiring soon etc, but for now just want to get this initial bit working in best way possible

    Form1.vb

    Imports Microsoft.Office.Interop.Outlook Imports Microsoft.Office.Interop Public Class DemoForm WithEvents bsContacts As New BindingSource Private Sub DemoForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim MyList As New List(Of MyContact) Dim bResult As Boolean = False Dim OutlookApp As Outlook.Application = Nothing Dim olNameSpace As Outlook.NameSpace Dim olConFolder As Outlook.MAPIFolder = Nothing Dim olConItems As Outlook.Items = Nothing Dim olConItem As Outlook.ContactItem Try OutlookApp = New Outlook.Application olNameSpace = OutlookApp.GetNamespace("MAPI") ' ' You may need to sign in, I have Outlook open while running this code ' 'oNameSpace.Logon( "", "", False, True) olConFolder = olNameSpace.GetDefaultFolder( Outlook.OlDefaultFolders.olFolderContacts) olConItems = olConFolder.Items For x As Integer = 1 To olConItems.Count - 2 If TypeOf olConItems(x) Is ContactItem Then Dim Item As ContactItem = CType(olConItems(x), ContactItem) ' If Not String.IsNullOrEmpty(Item.Email1DisplayName) AndAlso Not String.IsNullOrEmpty(Item.Email1Address) Then MyList.Add(New MyContact With { .ContactID = Item.EntryID, .FileAsName = Item.FileAs, .EmailAddress = Item.Email1Address, .MailingStatus = Item.UserProperties.Find("Reason for not mailing").Value, .MailingSent = Item.UserProperties.Find("Mailing Sent").Value, .DateOfLastContact = Item.UserProperties.Find("Date of last visit").Value, .Source = Item.UserProperties.Find("Source").Value, .PCHealthCheck = Item.UserProperties.Find("Date of Last PC Health Check").Value, .EmailSupport = Item.UserProperties.Find("Email Support expires").Value, .WebHosting = Item.UserProperties.Find("Web Hosting expires").Value, .SearchService = Item.UserProperties.Find("Search Service expires").Value } ) 'End If End If ' shows number of contacts found lblContactCount.Text = x Next Finally olConItem = Nothing olConItems = Nothing olConFolder = Nothing olNameSpace = Nothing OutlookApp = Nothing End Try ' binds the resulting MyList to the bsContacts data source bsContacts.DataSource = MyList ' shows customer "file as" name in the list box - list box is populated from the bsContacts data source lbContactName.DisplayMember = "FileAsName" lbContactName.DataSource = bsContacts ' shows details for customer selected in list box in relevent text boxes

    tbMailingStatus.DataBindings.Add("Text", bsContacts, "MailingStatus") tbMailingSent.DataBindings.Add("Text", bsContacts, "MailingSent", True, DataSourceUpdateMode.Never, "d") tbSource.DataBindings.Add("Text", bsContacts, "Source") tbDateOfLastContact.DataBindings.Add("Text", bsContacts, "DateOfLastContact", True, DataSourceUpdateMode.Never, "d") tbPCHealthCheck.DataBindings.Add("Text", bsContacts, "PCHealthCheck", True, DataSourceUpdateMode.Never, "d") tbEmailSupport.DataBindings.Add("Text", bsContacts, "EmailSupport", True, DataSourceUpdateMode.Never, "d") tbWebHosting.DataBindings.Add("Text", bsContacts, "WebHosting", True, DataSourceUpdateMode.Never, "d") tbSearchService.DataBindings.Add("Text", bsContacts, "SearchService", True, DataSourceUpdateMode.Never, "d") End Sub End Class


    MyContact.vb

    Public Class MyContact
        Public Property FileAsName As String
        Public Property EmailAddress As String
        Public Property ContactID As String
    
        Public Property MailingStatus As String
        Public Property MailingSent As DateTime
        Public Property DateOfLastContact As DateTime
        Public Property Source As String
        Public Property EmailSupport As DateTime
        Public Property WebHosting As DateTime
        Public Property SearchService As DateTime
        Public Property PCHealthCheck As DateTime
    
        Public Sub New()
        End Sub
        Public Overrides Function ToString() As String
            Return FileAsName
        End Function
    End Class


    Darren Rose

    Thursday, August 28, 2014 4:37 PM

Answers

  • 1. For Outlook a date of None is stored as 1/1/4501. To show anything else you'd need to see that data and replace it in your form with None wherever it appears.

    2. For x As Integer = 1 To olConItems.Count - 2 will return all but 2 of your items in the folder. Outlook's collections start at Count = 1 and run to Count. To get all items use For x As Integer = 1 To olConItems.Count

    That should answer your Outlook specific questions.


    Ken Slovak MVP - Outlook

    Thursday, August 28, 2014 6:34 PM
    Moderator

All replies

  • Hello Daren,

    The current forum is for Outlook specific questions. I'd suggest asking such questions in the Visual Basic forum instead.

    However, I see that you don't release underlying COM objects instantly. Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. Set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. You can read more about this in the Systematically Releasing Objects article in MSDN.

    Thursday, August 28, 2014 4:55 PM
  • Originally asked question on vb forum and someone moved it to outlook

    Will post this question on vb forum and see if it gets answered or moved again

    Thanks


    Darren Rose

    Thursday, August 28, 2014 5:11 PM
  • I don't see any Outlook specific questions in your original post above. Most probably somebody noticed Outlook mentioned and moved the post immediately without reading it to the end.
    Thursday, August 28, 2014 5:19 PM
  • 1. For Outlook a date of None is stored as 1/1/4501. To show anything else you'd need to see that data and replace it in your form with None wherever it appears.

    2. For x As Integer = 1 To olConItems.Count - 2 will return all but 2 of your items in the folder. Outlook's collections start at Count = 1 and run to Count. To get all items use For x As Integer = 1 To olConItems.Count

    That should answer your Outlook specific questions.


    Ken Slovak MVP - Outlook

    Thursday, August 28, 2014 6:34 PM
    Moderator
  • Thanks Ken

    1) Yes had worked that much out - just didn't know if easy way of modifying existing code

    2) Fixed that - did wonder about that myself as knew it was missing some of my contacts


    Darren Rose

    Thursday, August 28, 2014 7:44 PM