Search code examples
excelvbaappointment

Why would I get an Invalid or unqualified reference part way through an AppointItem?


The code stops running with .Body = "" highlighted and says it is an invalid or unqualified reference. Any insights are greatly appreciated.

Note that the variables that start with Appt_ are all global variables defined in another module.

Sub RegisterAppointmentList(CName As String)

' adds a list of appontments to the Calendar in Outlook
Dim olApp As Outlook.Application
Dim olAppItem As Outlook.AppointmentItem

Set olAppItem = Application.CreateItem(olAppointmentItem)

On Error Resume Next

Set olApp = GetObject("", "Outlook.Application")
On Error GoTo 0

If olApp Is Nothing Then
    On Error Resume Next
    Set olApp = CreateObject("Outlook.Application")
    On Error GoTo 0
    If olApp Is Nothing Then
        MsgBox "Outlook is not available!"
        Exit Sub
    End If
End If


olAppItem _
    .Location = ""
    .Body = ""
    .Start = Nothing
    .End = Nothing
    .ReminderSet = True
    .ReminderMinutesBeforeStart = 15
    .BusyStatus = olFree
    .RequiredAttendees = "[email protected]"
     On Error Resume Next
    .Start = Appt_STime
    .End = Appt_ETime
    .Subject = CName & "," & Appt_Subject
    .Location = Appt_Location
    .Body = Appt_Description
    .ReminderSet = True
    .BusyStatus = olBusy
     On Error GoTo 0
    .Save ' saves the new appointment to the default folder

Set olAppItem = Nothing
Set olApp = Nothing
MsgBox "Done !"
End Sub

Solution

  • If you want to change multiple properties, the best way to do it would be using "With":

    With olAppItem
        .Location = ""
        .Body = ""
        .Start = Nothing
        .End = Nothing
        .ReminderSet = True
        .ReminderMinutesBeforeStart = 15
        .BusyStatus = olFree
        .RequiredAttendees = "[email protected]"
         On Error Resume Next
        .Start = Appt_STime
        .End = Appt_ETime
        .Subject = CName & "," & Appt_Subject
        .Location = Appt_Location
        .Body = Appt_Description
        .ReminderSet = True
        .BusyStatus = olBusy
         On Error GoTo 0
        .Save ' saves the new appointment to the default folder
    End With