用于将Outlook 2003共享日历信息发送到Excel电子表格的代码

时间:2017-03-15 15:11:55

标签: excel vba outlook outlook-vba

我一直试图拼凑一些代码,将日历中的所有信息发送到Excel文件。我在多站点共享网络上工作,日历通常与需要从共享公司邮箱访问它们的每个人共享。

我已经找到了如何从本地日历和两个人之间共享的日历中做到这一点的指南,但我很难让它在我们的系统上运行。

以下是我正在使用的代码:

 Sub RunExportCalendarsToExcel()
    'Change the name of the conference room on the next line.  The name must match the name of the mailbox.'
    ExportCalendarToExcel "County, Family", True
End Sub

Sub ExportCalendarToExcel(strCalendarName As String, Optional bolClearWorksheet As Boolean)
    Dim olkFolder As Outlook.Folder, olkItems As Outlook.Items, olkAppt As Outlook.AppointmentItem, olkRecipient As Outlook.Recipient
    Dim excApp As Object, excWkb As Object, excSht As Object, excRng As Object, lngRow As Long
    Dim arrTitle As Variant

    'Launch Excel and open the spreadsheet'
    Set excApp = CreateObject("Excel.Application")
    excApp.Visible = True
    'Change the name and path of the spreadsheet on the next line'
    Set excWkb = excApp.Workbooks.Open("\\dom1\data\County\Wolverhampton\Home\[my username]\List.xls")
    Set excSht = excWkb.Worksheets(1)
    If bolClearWorksheet Then
        Set excRng = excSht.Range("A1").CurrentRegion
        lngRow = excRng.Rows.Count
        excApp.Rows(2 & ":" & lngRow).Delete
        lngRow = 2
    Else
        lngRow = excSht.UsedRange.Rows.Count + 1
    End If

   'Connect to and process the shared calendar'
    Set olkRecipient = Session.CreateRecipient(strCalendarName)
    Set olkFolder = Session.GetSharedDefaultFolder(olkRecipient, olFolderCalendar)
    datDate = InputBox("Enter the date you want to export for.", "Export Calendars to Excel", Date)
    Set olkItems = olkFolder.Items.Restrict("[Start] >= '" & Format(datDate & " 0:01am", "ddddd h:nn AMPM") & "' AND [Start] < '" & Format(datDate & " 11:59pm", "ddddd h:nn AMPM") & "'")
    olkItems.Sort "[Start]"
    olkItems.IncludeRecurrences = True
    For Each olkAppt In olkItems
        arrTitle = Split(olkAppt.Subject, "-")
        excSht.Cells(lngRow, 1) = olkAppt.Start
        excSht.Cells(lngRow, 2) = olkAppt.End
        excSht.Cells(lngRow, 3) = strCalendarName
        excSht.Cells(lngRow, 4) = olkAppt.Organizer
        excSht.Cells(lngRow, 5) = olkAppt.Body
        lngRow = lngRow + 1
    Next

    'Save the spreadsheet and exit Excel'
    Set excRng = Nothing
    Set excSht = Nothing
    excWkb.Save
    Set excWkb = Nothing
    excApp.Quit
    Set excApp = Nothing

    'Clean-up the Outlook objects'
    Set olkFolder = Nothing
    Set olkItems = Nothing
    Set olkAppt = Nothing
End Sub

在上午9点到下午5点之间,有三个日历在不同时间安排预约。

无论我做什么,我都会收到错误&#39;未定义的用户定义类型&#39;在这一行,用Dim olkFolder作为Outlook.Folder&#39;突出显示,好像它已被选中:

Sub ExportCalendarToExcel(strCalendarName As String, Optional bolClearWorksheet As Boolean)
Dim olkFolder As Outlook.Folder, olkItems As Outlook.Items, olkAppt As Outlook.AppointmentItem, olkRecipient As Outlook.Recipient

我只是在VBA写了几个月,所以如果还有其他任何我需要的信息,请告诉我。

1 个答案:

答案 0 :(得分:0)

2003 Outlook对象模型使用MAPIFolder作为对象名,而不是文件夹 - 这是在以后的版本中引入的(向后兼容MAPIFolder的用法)。