Microsoft Office Tutorials and References
In Depth Information
Dim MyOutlookNS As Outlook.NameSpace
Dim temp As String
Dim OneWeekHence As Date
Dim doc As Outlook.NoteItem
‘ Get a reference to the calendar folder.
Set MyOutlookNS = GetNamespace(“MAPI”)
Set MyCalendar = MyOutlookNS.GetDefaultFolder(olFolderCalendar)
‘ Create a date a week from now.
OneWeekHence = DateAdd(“d”, 7, Date)
‘ Create the Note heading
temp = “Week of “ & Date & vbCrLf & vbCrLf
‘ Loop for each appointment in the folder.
For Each MyAppt In MyCalendar.Items
‘ Find appointments within the next week.
If MyAppt.Start >= Date And _
MyAppt.Start <= OneWeekHence Then
‘ The info is put together in temp.
temp = temp & MyAppt.Subject & vbCrLf
temp = temp & “ Date: “ & _
Format(MyAppt.Start, “Medium Date”) & vbCrLf
temp = temp & “ When: “ & Format(MyAppt.Start, “Medium Time”) _
temp = temp & “ Ends: “ & Format(MyAppt.End, “Medium Time”) _
temp = temp & “ Where: “ & MyAppt.Location & vbCrLf & vbCrLf
‘ Create a new note.
Set doc = CreateItem(olNoteItem)
‘ Add the compiled text to the Note.
doc.Body = temp
‘ Display the note.
Macros and VBA programming let you automate commonly performed tasks in Outlook. Although it
takes some time and effort to create the macros, you’ll often find that this investment is generously
repaid in saved time and reduced errors. This chapter presented two real-world examples of VBA code
that performs useful, real-world tasks. These applications can serve as the basis for your own projects.