简体   繁体   English

VBA Excel创建Outlook 2013约会

[英]VBA Excel create Outlook 2013 Appointment

I am trying to create an Outlook Appointment with a Macro, I am having an issue with the code giving me an error of "Object Doesn't support this property or method" when it comes the Default Appointment Values. 我正在尝试使用宏创建Outlook约会,但是默认代码中出现“对象不支持此属性或方法”的错误,代码出现问题。 I have tried several fixes but am not experienced enough to resolve. 我已经尝试了一些修复程序,但经验不足以解决。 Any assistance is greatly appreciated. 非常感谢您的协助。

Here is what I am working with: 这是我正在使用的:

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
 Set olAppItem = olApp.CreateItem(olAppointmentItem) 

Assuming you're late-binding the Outlook library, the constant olAppointmentItem is not defined, so if you specify Option Explicit at the top of the module the VBE will highlight if as undeclared. 假设您在后期绑定Outlook库,则未定义常量olAppointmentItem ,因此,如果在模块顶部指定Option Explicit ,则VBE将在未声明的情况下突出显示。

I copied your code into ThisWorkbook in an empty workbook, and ran Rubberduck code inspections (disclaimer: I manage that open-source project; it's completely free, and it's improving every day). 我将您的代码复制到一个空工作簿中的ThisWorkbook中,并进行了Rubberduck代码检查(免责声明:我管理该开源项目;它是完全免费的,并且每天都在改进)。

代码检查结果

These results are particularly relevant to your problem: 这些结果与您的问题特别相关:

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

The underlying value of olFree in the Outlook library is 0 , so that's not a big deal as far as run-time errors are concerned. Outlook库中olFree的基础值为0 ,因此就运行时错误而言,这并不重要

However olAppointmentItem not being defined is pretty big: you think you're working against an AppointmentItem object, but because the underlying value of olAppointmentItem in the Outlook library is 1 and you're providing a 0 , the runtime type of olAppItem is actually a MailItem . 然而olAppointmentItem没有被定义相当大的:你认为你是对一个工作AppointmentItem对象,但由于潜在价值olAppointmentItem在Outlook库是1 ,你要提供一个0 ,的运行时类型olAppItem实际上是一个MailItem

And because a MailItem doesn't have a Location property, attempting to set it will raise that run-time error 438 you're getting - "object doesn't support this property or method". 而且由于MailItem没有Location属性,尝试对其进行设置将引发您所遇到的运行时错误438-“对象不支持此属性或方法”。

Therefore, you should be creating the olAppItem like this: 因此,您应该像这样创建olAppItem

Set olAppItem = olApp.CreateItem(1)

Or, define the olAppointmentItem constant: 或者,定义olAppointmentItem常量:

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

Or, reference the Outlook object model ( Tools > References... ), replace As Object with the actual types you want to be using ( olApp As Outlook.Application , olAppItem As AppointmentItem ), and then the olAppointmentItem and olFree constants will be taken from the Outlook library. 或者,引用Outlook对象模型( 工具>参考... ),将As Object替换As Object您要使用的实际类型( olApp As Outlook.ApplicationolAppItem As AppointmentItem ),然后将使用olAppointmentItemolFree常量从Outlook库。

I'll skip the other inspection results because they're not relevant to that specific question, but you'll notice a number of dead variables there. 我将跳过其他检查结果,因为它们与特定问题无关,但是您会注意到那里有许多无效变量。

If you want to create an appointment in Outlook, using Excel, run the script below. 如果要使用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

在此处输入图片说明

' The code comes from this link: http://officetricks.com/add-appointment-to-outlook-calendar-through-excel-macro-vba/ '该代码来自此链接: http : //officetricks.com/add-appointment-to-outlook-calendar-through-excel-macro-vba/

The script is run from Excel, and as such, you must set a reference to Outlook before you run the code. 该脚本是从Excel运行的,因此,在运行代码之前必须设置对Outlook的引用。 Also, notice that the worksheet needs to be setup properly for the script to run. 另外,请注意,需要正确设置工作表才能运行脚本。 It should look something like this. 它看起来应该像这样。 Everything is read from Excel into Outlook. 一切都从Excel读入Outlook。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM