如何使用 Visual Basic 将 Microsoft Access 约会字段与 Outlook 日历同步

问题描述 投票:0回答:1

我需要在 Access 数据库和 Outlook 之间创建约会链接。 我有一个表格,可以在其中输入所有约会详细信息,例如主题和日期。我创建了一个宏,每次数据库更新(更新后)时都会运行我的编码函数,并且我使用了以下代码:

Public Function SyncWithOutlook()
    Dim objOL As Object
    Dim olNS As Object
    Dim olFolder As Object
    Dim olAppt As Object
    Dim olApptExists As Boolean

    ' Create Outlook Application object
    Set objOL = CreateObject("Outlook.Application")

    ' Get the MAPI namespace
    Set olNS = objOL.GetNamespace("MAPI")

    ' Get the default Calendar folder
    Set olFolder = olNS.GetDefaultFolder(olFolderCalendar)

    ' Check if Appointment already exists
    For Each olAppt In olFolder.Items
        If Me.subject.text = olAppt.subject Then
            ' Appointment already exists, update its properties
            olAppt.subject = Me.subject.text
            olAppt.startDate = Me.startDate.Value
            olAppt.endDate = Me.endDate.Value
            olAppt.location = Me.location.text
            olAppt.AllDayEvent = Me.chkAllDay
            olAppt.Save
            olApptExists = True
            Exit For
        End If
    Next olAppt

    ' If appointment doesn't exist, create a new one
    If Not olApptExists Then
        Set olAppt = olFolder.Items.Add
        With olAppt
            .subject = Me.subject.text
            .startDate = Me.startDate.Value
            .endDate = Me.endDate.Value
            .location = Me.location.text
            .AllDayEvent = Me.chkAllDay
            .Save
        End With
    End If

    ' Clean up
    Set olAppt = Nothing
    Set olFolder = Nothing
    Set olNS = Nothing
    Set objOL = Nothing
    Set olApptExists = Nothing
End Function

我还确保包含 Outlook 对象数据库,但如果我在数据库中输入约会,则不会发生任何情况。知道问题出在哪里吗?

我已经尝试了同一代码的不同版本,还尝试查找代码中的任何对象变量是否存在问题,但我找不到任何东西

vba ms-access outlook office365
1个回答
0
投票

Outlook 有时在处理真实的日期时间值时遇到问题。所以尝试使用字符串表达式:

olAppt.startDate = Format(Me.startDate.Value, "Short Date") & " " & Format(Me.startDate.Value, "Short Time")
olAppt.endDate = Format(Me.endDate.Value, "Short Date") & " " & Format(Me.endDate.Value, "Short Time")
© www.soinside.com 2019 - 2024. All rights reserved.