Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / productivity / Office

MiniCRM

3.67/5 (6 votes)
4 Jan 2008CPOL4 min read 1   613  
Notify your contacts with this Outlook 2007 add-in.

Introduction

For many occasions such as holidays or business events, I needed to send emails to my contacts without putting all the contact names in the To field of a single message. At many times, I needed to send a copy of a certain message to my contacts individually and I had to go through repeating the process of opening a message, pasting the body, entering the contact email and sending it.

Then I was introduced to Visual Studio Tools For Office (VSTO). I thought of creating an add-in for Outlook 2007 that reads my contacts from a database and sends a message to them one by one. This message will be entered in the Outlook editor as usual, including the subject and body. All that I have to do is press the Send button and this composed message is sent to my contacts stored previously in an Access database. This is not meant for SPAM since the sender email is retrieved from the current Outlook account and not faked by the user manually.

Please download the ZIP file and open the project in Visual Studio .NET 2008. Build and run. Outlook 2007 will be launched. To view the add-in, do the following:

  1. Open the contacts.mdb file located in the project folder and enter the information of your contacts. The most important data is the name and email address.
  2. Invoke the New Message Compose dialog.
  3. Select the CppMax Ribbon at the top left and then click the MiniCRM link below it to the right.
  4. A list of your customers (stored previously in contacts.mdb, located in the project folder) will appear in the left task pane.
  5. Enter the subject and body of your message.
  6. Click the Send Emails button at the bottom of the task pane below the customer list.

The Compose dialog will be closed and the new message will be sent to all recipients in the list.

How?

To introduce yourself to the VSTO add-in, I suggest you watch the special webcast on Microsoft's VSTO site. I see this as a good starting point. The sample shown in this article was written after watching couple of those webcasts. I am not going to repeat what has been said in them, but I will highlight the steps needed to do add-ins for Outlook:

  1. Create the Project: File/Add/New Project. Select the Visual C# or VB.NET Office tree node. Select Outlook 2007 Add-in and enter the project name and path.
  2. Add a data source using Data/Add New Data Source. Select the database type and name in the wizard. Follow the wizard to select your fields and create the query.
  3. Create a new task pane. A task pane is a dockable window that appears to one side of the current Outlook window. A task pane is actually nothing more than a User Control. Deal with the User Control the same way you deal with a Windows Forms User Control. Add controls, events and .NET code to it as usual.
  4. Add a Ribbon: Project/Add/New Item/Ribbon XML. Edit the XML to specify your ribbon tab, group and buttons. Write the button event handlers to invoke the task pane or do anything you want within Outlook.
  5. Retrieve the data using the defined data source or through direct code and display the data within a grid or any data-bound control.
  6. This is all usual so far. What is different about this add-in is how it sends emails to all recipients one by one. Here is the code to do that:
    VB
    'In the class declaration of the User Control
    Private contact As ContactsDataSet.ContactsRow
    Private thisInspector As Outlook.Inspector
    Private thisMessage As Outlook.MailItem
    
    // The method handler to send the emails
    Private Sub SendEmails_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles SendEmails.Click
        'The Current Inspector is the Mail Compose New Message dialog
        thisInspector = Globals.ThisAddIn.Application.ActiveInspector
        thisMessage = CType(thisInspector.CurrentItem, Outlook.MailItem)
    
        Dim contact As ContactsDataSet.ContactsRow
        Dim sSubject As String
        Dim sBody As String
        Dim msg As Outlook.MailItem
    
    
        ' save the message body and subject
        sSubject = thisMessage.Subject
        sBody = thisMessage.Body
        ' loop for all contacts in the database
        For Each contact In ContactsDataSet.Tables(0).Rows
            ' create a new message and set the body and subject then send
            msg = CType(Globals.ThisAddIn.Application.CreateItem(_
                Outlook.OlItemType.olMailItem), Outlook.MailItem)
            msg.Subject = sSubject
            msg.Body = sBody
            msg.To = contact.EmailAddress
            msg.Send()
        Next
        ' close the Mail Compose dialog
        thisMessage.Close(Outlook.OlInspectorClose.olDiscard)
    End Sub
  7. Note that the add-in is run from within Visual Studio .NET 2008 and not directly through a setup. This is because, to have a standalone final add-in, you need to sign it due to the strong security features in Office.
  8. A setup project is included to install the sample, but it may not run because the code needs to be authenticated and signed.

Future Enhancements

Much work needs to be done for this sample and it is not a final product. It is just to show you how. In the future, we may add Adding and Deleting to the Customer List directly from Outlook (and not just through Access). Also, importing from a text file is also possible.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)