Outlook VBA

Microsoft Outlook VBA Code Samples

Here are links to my Outlook VBA code samples. There are many more on my blog. I also have some generic Outlook VBA routines below.

To assign a macro to a toolbar button in Outlook, see How to assign a macro to a toolbar button.

Don't forget to check out the Video Tutorials page!

Periodically you will see new articles added to this section. To receive notification when new articles are published, subscribe to the site feed.

Lunch Special

    A routine (that works in Excel as well) that pops up a MsgBox with a random msg. In this case, it's a fun app that helps me choose what to eat for lunch. I can see many other uses for this code. I use the same code in the Application_Startup event to pop up a random msg when Outlook starts.

Sub PickLunchForMe()
Dim X As Integer
Randomize
X = Int((5 - 1) * Rnd + 1)
 
Select Case X
    Case 1
        MsgBox "Deli"
    Case 2
        MsgBox "Spanish food"
    Case 3
        MsgBox "Chinese food"
    Case 4
        MsgBox "Diner (American food)"
    Case 5
        MsgBox "Tex Mex"
    Case Else
End Select
 
End Sub

    I attach this code to a toolbar button to run it on demand. If you have a different number of options, simply change the "5" in the code to whatever number you want. For example, if you had 10 options, the code would read X = Int((10 – 1) * Rnd + 1). Then you would need to add additional "Case" statements as appropriate. Hopefully your meal choices are better than mine.

Create Excel Workbook (From Outlook)

    This is a simple routine that generates a blank Excel workbook from Outlook. Optional: set a reference to the Excel application library from the Outlook VBIDE. You don't have to set the workbook Visible property to True if you want to write anything to it. At this point you can you use any Excel VBA techniques on the workbook — they work the same as if we were operating in the Excel VBIDE.

Sub CreateXLWorkbook()
Dim xlApp As Object ' Excel.Application
Dim xlWkb As Object ' Excel.Workbook

Set xlApp = CreateObject("Excel.Application") ' New Excel.Application

Set xlWkb = xlApp.Workbooks.Add

xlApp.Visible = True

Set xlApp = Nothing
Set xlWkb = Nothing

End Sub

Site last updated March 9, 2010 @ 8:20 pm; This content last updated February 9, 2010 @ 6:12 pm