VBA Excel创建Outlook 2013约会

时间:2017-02-24 17:22:30

标签: excel vba excel-vba outlook

我正在尝试使用宏创建Outlook约会,我遇到了一个问题,在默认约会值出现时,我给出了“对象不支持此属性或方法”的错误。我已经尝试了几个修复,但没有足够的经验来解决。非常感谢任何帮助。

以下是我的工作内容:

Sub CalendarInvite()

    Dim olApp As Object
    Dim olAppItem As Object
    Dim r As Long

    Set olApp = GetObject("", "Outlook.Application")

    Dim mysub, myStart, myEnd

        mysub = Range("Title")
        myStart = Range("Date")
        myEnd = Range("Date")

        'creates a new appointment
        Set olAppItem = olApp.CreateItem(olAppointmentItem)

        'set default appointment values
        With olAppItem
            .Location = Range("Location")
            .Body = Range("Body")
            .ReminderSet = True
            .BusyStatus = olFree
            .RequiredAttendees = "email@email.com"
            'saves the new appointment to the default folder
            .Save
        End With

    Set olAppItem = Nothing
    Set olApp = Nothing

End Sub

2 个答案:

答案 0 :(得分:2)

Set olAppItem = olApp.CreateItem(olAppointmentItem)

假设您对Outlook库进行了后期绑定,则未定义常量olAppointmentItem,因此如果您在模块顶部指定Option Explicit,则VBE将突出显示是否为未声明的。

我将代码复制到ThisWorkbook空白工作簿中,并运行Rubberduck代码检查(免责声明:我管理该开源项目;它完全免费,并且每天都在改进)。< / p>

code inspection results

这些结果与您的问题特别相关:

Error: Option Explicit is not specified in 'ThisWorkbook' - (Book3) VBAProject.ThisWorkbook, line 1
Error: Variable 'olAppointmentItem' is used but not assigned - (Book3) VBAProject.ThisWorkbook, line 16
Error: Variable 'olFree' is used but not assigned - (Book3) VBAProject.ThisWorkbook, line 23
Error: Local variable 'olAppointmentItem' is not declared - (Book3) VBAProject.ThisWorkbook, line 16
Error: Local variable 'olFree' is not declared - (Book3) VBAProject.ThisWorkbook, line 23

Outlook库中olFree的基础值为0,因此就运行时错误而言,这不是交易。

olAppointmentItem未定义 非常大:您认为自己正在对AppointmentItem对象进行操作,但因为olAppointmentItem的基础值Outlook库为1,您提供0,运行时类型olAppItem实际上是MailItem

因为MailItem没有Location属性,尝试设置它会引发你得到的运行时错误438 - “对象不支持此属性或方法”

因此,您应该像这样创建olAppItem

Set olAppItem = olApp.CreateItem(1)

或者,定义olAppointmentItem常量:

Const olAppointmentItem As Long = 1
Set olAppItem = olApp.CreateItem(olAppointmentItem)

或者,引用Outlook对象模型(工具&gt;引用... ),将As Object替换为您要使用的实际类型(olApp As Outlook.Application,{ {1}}),然后将从Outlook库中获取olAppItem As AppointmentItemolAppointmentItem常量。

我会跳过其他检查结果,因为它们与那个具体问题无关,但你会注意到那里有一些死变量。

答案 1 :(得分:0)

如果要使用Excel在Outlook中创建约会,请运行以下脚本。

Private Sub Add_Appointments_To_Outlook_Calendar()

    'Include Microsoft Outlook nn.nn Object Library from Tools -> References
    Dim oAppt As AppointmentItem
    Dim Remind_Time As Double

    i = 2
    Subj = ThisWorkbook.Sheets(1).Cells(i, 1)

    'Loop through entire list of Reminders to be added
    While Subj <> ""
        Set oAppt = Outlook.Application.CreateItem(olAppointmentItem)

        oAppt.Subject = Subj
        oAppt.Location = ThisWorkbook.Sheets(1).Cells(i, 2)
        oAppt.Start = ThisWorkbook.Sheets(1).Cells(i, 3)
        Remind_Time = ThisWorkbook.Sheets(1).Cells(i, 4) * 1 * 60
        oAppt.ReminderMinutesBeforeStart = Remind_Time
        oAppt.AllDayEvent = True
        oAppt.Save

        i = i + 1
        Subj = ThisWorkbook.Sheets(1).Cells(i, 1)
    Wend
    MsgBox "Reminder(s) Added To Outlook Calendar"

End Sub

enter image description here

&#39;代码来自此链接: http://officetricks.com/add-appointment-to-outlook-calendar-through-excel-macro-vba/

脚本从Excel运行,因此,在运行代码之前,必须设置对Outlook的引用。另请注意,需要正确设置工作表才能运行脚本。看起来应该是这样的。一切都从Excel读入Outlook。