none
BeforeCheckNames Event for dummies RRS feed

  • Question

  • Hello, 

    I would like to create an event for "Resolve Names" function in Outlook. I'm using Visual Studio 2012 and VB.NET. 

    I found the code for VBA: 

    http://msdn.microsoft.com/en-us/library/office/aa209978(v=office.11).aspx

    and MSDN description for the event for VB.NET: 

    http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.itemevents_10_event.beforechecknames.aspx

    However It seems I do not have sufficient knowledge to implement the solution within my code (I am quite e beginner).

    All I want to achieve is to add to the begining of body message a display name of recipients. 

    Could you please guide me or support with code example how it could be achieved? 

    Thank you in advance, 

    Best regards, 

    Maciej

    Saturday, September 14, 2013 9:02 AM

Answers

  • The example VBA code cannot just be put into a VB.NET addin and have it work.

    If your addin is a VSTO addin, you have a Startup event provided by VSTO. You need to grab the Outlook.Application object in that event and set a variable to that object at a scope where it will not be garbage collected.

    It would look something like this in the ThisAddin class:

    Public WithEvents myItem As Outlook.MailItem
    Public olApp As Outlook.Application

    Private Sub myItem_BeforeCheckNames(Cancel As Boolean)
    If MsgBox("Do you want to resolve names now?", 4) = vbOK Then
    Cancel = True
    End If
    End Sub

        Private Sub ThisAddIn_Startup() Handles Me.Startup
            olApp = Me.Application

            myItem = olApp.CreateItem(olMailItem)

             myItem.Recipients.Add ("Dan Wilson")
             myItem.Recipients.Add ("Nate Sun")

             myItem.Recipients.ResolveAll() ' this will fire the event handler    

             myItem.Body = "Good morning!"

             myItem.Send

    End Sub
    Now bear in mind that this code will fail unless you have Dan Wilson and Nate Sunin your Outlook address book, and all it does is to send an email when Outlook starts the addin.

    The BeforeCheckNames() event will fire when ResolveAll() is called. It would also fire when the item is sent, but personally I'd rather have it fire where I indicated.

    I'd recommend that you download the example COM addins from the Office Developer Web site at MSDN and study them to see how they are written and how they work. There are samples in VB.NET.

    I'd also recommend going to www.outlookcode.com, that site has a ton of information on programming Outlook. Finally, if you're not at all familiar with Outlook programming I'd recommend a book. For beginners I'd recommend anything by Sue Mosher. Her books are referenced on the outlookcode.com Web site. Anything by Randy Byrne or myself are more advanced and assume a certain level of preexisting programming knowledge.


    Ken Slovak MVP - Outlook

    Friday, September 20, 2013 2:36 PM
    Moderator

All replies

  • The VBA code shows how to handle the event, are you using code like that? What specific problem are you having?

    Where is your code running? Is it a COM addin, a standalone EXE, and Windows forms app, ???


    Ken Slovak MVP - Outlook

    Monday, September 16, 2013 2:27 PM
    Moderator
  • Hello, 

    Thank you for your reply. I used Outlook 2010 Add-in. When I just copy-paste the VBA code it didn't work for me. Could you please just confirm that this should work? If yes I will at least know that this is a good & proper way to code it. 

    I was trying also to use a VB.NET way presented in a second link, however there is no description how & where should I capture the event. 

    I am aware that it may not sounds like a professional question, but I do really have such need and I do not really know how do it. I would really appreciate your support, I believe there may others who can leverage from this since I did not found any easy-to-follow steps how capturing this event can be accomplished.

    Again, if VBA code presented above is this what I'm looking for, please just confirm it - it will help me to investigate what is wrong on my machine. It would be also great if you could put some light on this VB.NET solution and capturing event which is added by AddHandler method. 

    Thank you in advance for any kind of support here. 

    Thursday, September 19, 2013 6:32 PM
  • The example VBA code cannot just be put into a VB.NET addin and have it work.

    If your addin is a VSTO addin, you have a Startup event provided by VSTO. You need to grab the Outlook.Application object in that event and set a variable to that object at a scope where it will not be garbage collected.

    It would look something like this in the ThisAddin class:

    Public WithEvents myItem As Outlook.MailItem
    Public olApp As Outlook.Application

    Private Sub myItem_BeforeCheckNames(Cancel As Boolean)
    If MsgBox("Do you want to resolve names now?", 4) = vbOK Then
    Cancel = True
    End If
    End Sub

        Private Sub ThisAddIn_Startup() Handles Me.Startup
            olApp = Me.Application

            myItem = olApp.CreateItem(olMailItem)

             myItem.Recipients.Add ("Dan Wilson")
             myItem.Recipients.Add ("Nate Sun")

             myItem.Recipients.ResolveAll() ' this will fire the event handler    

             myItem.Body = "Good morning!"

             myItem.Send

    End Sub
    Now bear in mind that this code will fail unless you have Dan Wilson and Nate Sunin your Outlook address book, and all it does is to send an email when Outlook starts the addin.

    The BeforeCheckNames() event will fire when ResolveAll() is called. It would also fire when the item is sent, but personally I'd rather have it fire where I indicated.

    I'd recommend that you download the example COM addins from the Office Developer Web site at MSDN and study them to see how they are written and how they work. There are samples in VB.NET.

    I'd also recommend going to www.outlookcode.com, that site has a ton of information on programming Outlook. Finally, if you're not at all familiar with Outlook programming I'd recommend a book. For beginners I'd recommend anything by Sue Mosher. Her books are referenced on the outlookcode.com Web site. Anything by Randy Byrne or myself are more advanced and assume a certain level of preexisting programming knowledge.


    Ken Slovak MVP - Outlook

    Friday, September 20, 2013 2:36 PM
    Moderator