Here are three things I was unable to find any guidance for on the internets regarding working with Outlook 2007 in Visual Basic, so I made it up.
1. How do you tell if a folder is a contacts folder?
I still dont know if this is the correct way – I may not have found the right search terms, but this works.
Check the DefaultItemType property. If it is Outlook.OlItemType.olContactItem, then the folder is a contacts folder.
2. How do you make a toolbar context aware / context sensitive? That is, how do I have a toolbar that is only displayed when viewing contacts, or the calendar, but not email items?
You will need to create an event handler for the FolderSwitch event on the active explorer. In this handler, check the DefaultItemType of the CurrentFolder on the active explorer.
A code block is worth a thousand words:
Public Class ThisAddIn Dim ToolBar As Office.CommandBar Dim WithEvents expl As Outlook.Explorer Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup expl = Me.Application.ActiveExplorer ToolBar = expl.CommandBars.Add("ToolBar", Office.MsoBarPosition.msoBarTop, False, True) ToolBar.enabled = true End Sub Private Sub expl_FolderSwitch() Handles expl.FolderSwitch If expl.CurrentFolder.DefaultItemType = Outlook.OlItemType.olContactItem Then ToolBar.Visible = True Else ToolBar.Visible = False End If End Sub End Class
3. How do you create a Distribution List (aka DistListItem) in a non-default, custom folder? (Or how do you set the parent folder on a newly created Distribution List)?
All the examples I could find for creating Distribution lists programmatically suggest using Me.Application.CreateItem(Outlook.OlItemType.olDistributionListItem) to create the DistListItem object. When you do it this way, it always adds the Distribution list to the default contacts folder.
Turns out, you can also use <folder object>.Items.Add(Outlook.OlItemType.olDistributionListItem). You should be careful that this is only done in a contacts folder (can you see how this is all coming together?).
So, if you want to create a new distribution list in the current folder (instead of the default contacts folder), add a person to it, then present it to the user to save or disgard, you could do the following:
Private Sub createDistList() Dim dlist As Outlook.DistListItem Dim recip As Outlook.Recipient dlist = Me.Application.ActiveExplorer.CurrentFolder.Items.Add(Outlook.OlItemType.olDistributionListItem) recip = Me.Application.Session.CreateRecipient("John Doe <email@example.com>") recip.Resolve() dlist.AddMember(recip) dlist.display End Sub
I’ll let you figure out how to add the button, and create a handler for said button.
Hopefully this saves somebody some time. I find it hard to believe I’m the first person to try it…