简体   繁体   English

从 Excel 向 Outlook 2010 添加约会

[英]Adding an appointment to Outlook 2010 from Excel

I am trying to use the code below to update my Outlook calendar from an Excel sheet.我正在尝试使用下面的代码从 Excel 工作表更新我的 Outlook 日历。
The code functions fine, but I need to save to a sub calendar rather than my default one.代码运行良好,但我需要保存到子日历而不是我的默认日历。
I've tried a few work around's I found online,but none of them seem to work.我已经尝试了一些我在网上找到的解决方法,但它们似乎都不起作用。 For example Slapstick and also at the bottom of this page Ozgrid例如Slapstick以及本页底部的Ozgrid
Any help would be much appreciated.任何帮助将不胜感激。

Option Explicit
Sub AddToOutlook()


Dim OL As Outlook.Application
Dim olAppt As Outlook.AppointmentItem
Dim NS As Outlook.Namespace
Dim colItems As Outlook.Items
Dim olApptSearch As Outlook.AppointmentItem
Dim r As Long, sBody As String, sSubject As String, sLocation As String
Dim dStartTime As Date, dEndTime As Date, dReminder As String, dCatagory As Double
Dim sSearch As String, bOLOpen As Boolean

On Error Resume Next
Set OL = GetObject(, "Outlook.Application")
bOLOpen = True
If OL Is Nothing Then
    Set OL = CreateObject("Outlook.Application")
    bOLOpen = False
End If
Set NS = OL.GetNamespace("MAPI")
Set colItems = NS.GetDefaultFolder(olFolderCalendar).Items


For r = 2 To 394

    If Len(Sheet1.Cells(r, 1).Value + Sheet1.Cells(r, 5).Value) = 0 Then 
    GoTo NextRow
    sBody = Sheet1.Cells(r, 7).Value
    sSubject = Sheet1.Cells(r, 3).Value
    dStartTime = Sheet1.Cells(r, 1).Value + Sheet1.Cells(r, 2).Value
    dEndTime = Sheet1.Cells(r, 1).Value + Sheet1.Cells(r, 5).Value
    sLocation = Sheet1.Cells(r, 6).Value
    dReminder = Sheet1.Cells(r, 4).Value

    sSearch = "[Subject] = " & sQuote(sSubject)
    Set olApptSearch = colItems.Find(sSearch)


    If olApptSearch Is Nothing Then
        Set olAppt = OL.CreateItem(olAppointmentItem)
        olAppt.Body = sBody
        olAppt.Subject = sSubject
        olAppt.Start = dStartTime
        olAppt.End = dEndTime
        olAppt.Location = sLocation
        olAppt.Catagory = dCatagory
        olAppt.Close olSave
    End If


NextRow:
Next r


If bOLOpen = False Then OL.Quit


End Sub


Function sQuote(sTextToQuote)
sQuote = Chr(34) & sTextToQuote & Chr(34)
End Function

To get access to a subfolder in your default calendar folder you can use:要访问默认日历文件夹中的子文件夹,您可以使用:

Set colItems = NS.GetDefaultFolder(olFolderCalendar).Folders("TypeNameOfCalendarHere").Items

If it is on the same level as teh default folder you can use:如果它与默认文件夹在同一级别,您可以使用:

Set colItems = NS.GetDefaultFolder(olFolderCalendar).Parent.Folders("SharedCal").Items

Good resource here and here . 这里这里的好资源。

As described in the Ozgrid link, move the appointment created in the default calendar to the sub calendar.Ozgrid链接中所述,将在默认日历中创建的约会移至子日历。

You can reference a calendar with the entry ID.您可以使用条目 ID 引用日历。

Set oFolder = oNameSpace.GetFolderFromID("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")   

You can reference a sub Calendar of the default folder:您可以引用默认文件夹的子日历:

Set oFolder = NS.GetDefaultFolder(olFolderCalendar).folders("Name of sub calendar")

Once created in the default calendar move it to the non-default calendar在默认日历中创建后将其移动到非默认日历

Set olApt = oApp.CreateItem(olAppointmentItem)
With olApt
    ' ..
    .Save
    .Move oFolder
End With

You may add to a non-default calendar.您可以添加到非默认日历。

Set subCalendar = NS.GetDefaultFolder(olFolderCalendar).folders("Name of sub calendar")
Set olAppt = subCalendar.items.Add

With olAppt
     '...
     .Save
End With

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

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