Board logo

標題: 請問一下VBA可以抓特定日期範圍的OUTLOOK行事曆回來嗎? [打印本頁]

作者: yws0915    時間: 2021-3-15 14:06     標題: 請問一下VBA可以抓特定日期範圍的OUTLOOK行事曆回來嗎?

POWERQUERY取資料速度比較慢,範圍也很大
不曉得VBA能不能直接存取?
作者: yws0915    時間: 2021-3-15 22:41

https://www.mrexcel.com/board/threads/outlook-calendar-vba.816885/


有找到這個範例,可是它抓到"行事曆",我想抓的是"共用行事曆"
請問要修改那裡?
作者: yws0915    時間: 2021-3-16 14:00

我己經抓到想要的行事曆了~~
作者: yws0915    時間: 2021-3-16 14:50

  1. Public Sub ListAppointments()
  2. On Error GoTo ErrHand:

  3.     Application.ScreenUpdating = False

  4.     'This is an enumeration value in context of getDefaultSharedFolder
  5.     Const olFolderCalendar As Byte = 9

  6.     Dim olApp       As Object: Set olApp = CreateObject("Outlook.Application")
  7.     Dim olNS        As Object: Set olNS = olApp.GetNamespace("MAPI")
  8.     Dim olFolder    As Object
  9.     Dim olApt       As Object
  10.     Dim objOwner    As Object: Set objOwner = olNS.CreateRecipient("自己的EMAIL")
  11.     Dim NextRow     As Long
  12.     Dim ws          As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
  13. Dim FromDate As Date
  14.     Dim ToDate As Date
  15.    
  16.    
  17.     objOwner.Resolve

  18.     If objOwner.Resolved Then
  19.         Set olFolder = olNS.GetSharedDefaultFolder(objOwner, olFolderCalendar).Folders("共用行事曆")
  20.     End If

  21.     ws.Range("A1:D1").Value2 = Array("Subject", "Start", "End", "Location")

  22.     'Ensure there at least 1 item to continue
  23.     If olFolder.Items.Count = 0 Then Exit Sub

  24.     'Create an array large enough to hold all records
  25.     Dim myArr() As Variant: ReDim myArr(0 To 3, 0 To olFolder.Items.Count - 1)

  26.     'Add the records to an array
  27.     'Add this error skip, as I found some of my calendar items don't have all properties e.g. a start time
  28.     On Error Resume Next
  29.    FromDate = InputBox("Enter the start date (format: yyyy/mm/dd)")
  30.    ToDate = InputBox("Enter the end date(format: yyyy/mm/dd)")
  31.    For Each olApt In olFolder.Items
  32.     If (olApt.Start >= FromDate And olApt.Start <= ToDate) Then
  33.         myArr(0, NextRow) = olApt.Subject
  34.         myArr(1, NextRow) = olApt.Start
  35.         myArr(2, NextRow) = olApt.End
  36.         myArr(3, NextRow) = olApt.Categories
  37.         NextRow = NextRow + 1
  38.         Else
  39.         End If
  40.     Next
  41.     On Error GoTo 0

  42.     'Write all records to a worksheet from an array, this is much faster
  43.     ws.Range("A2:D" & NextRow + 1).Value = WorksheetFunction.Transpose(myArr)

  44.     'AutoFit
  45.     ws.Columns.AutoFit

  46. cleanExit:
  47.     Application.ScreenUpdating = True
  48.     Exit Sub

  49. ErrHand:
  50.     'Add error handler
  51.     Resume cleanExit
  52. End Sub
複製代碼
[attach]33137[/attach]

以上這樣就能取出共用行事曆裡的資料
作者: yws0915    時間: 2021-3-17 12:03

請問一下myArr(4, NextRow) = 要增加一個AllDayEvent要怎麼寫?
剛發現全天事件的結束日期會多了一天出來,想抓全天事件回來再改寫
可是不會寫




歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)