非版本特定 Excel VBA 代码的后期绑定

Late binding for non version specific Excel VBA code

我有一个 Excel 电子表格,我的 VBA 代码从中提取该电子表格以在共享的 Outlook 日历上创建约会。它将被多个项目经理操纵(当然不是同时)。

我发现一些用户使用旧版本的 Office,因此我了解到我应该使用“后期绑定”来使其与旧版本兼容。

如何将我拥有的内容转换为后期绑定。我必须创建此代码的所有示例都是早期绑定。

Option Explicit
    
Sub SCHMTG() 'Schedule Meeting
    
    Dim wb As Workbook
    Set wb = ThisWorkbook
    
    Dim ws As Worksheet
    Set ws = wb.Sheets("Projects")
    
    ws.Unprotect ""
    
    Dim check As Boolean
    check = False
    
    Dim o As Outlook.Application
    Set o = New Outlook.Application
    
    Dim oNS As Outlook.Namespace
    Set oNS = o.GetNamespace("MAPI")
    
    Dim FOL As Outlook.MAPIFolder
    Set FOL = oNS.GetFolderFromID("00000000F4EFC638C1F878469E872F63F51D794A0100F96BCFC3DAF87B4F8C66193C3EA6F4F40000029DA2430000")
    
    Dim oAPT As Outlook.AppointmentItem
    Dim oAPT_DATE As Date
    Dim oAPT_TIME As Date
    Dim oOBJECT As Object
    Dim b As CheckBox
    Dim r As Integer
    Dim c As Integer
    
    Set b = ws.CheckBoxes(Application.Caller)
    With b.TopLeftCell
        r = .Row
        c = .Column
    End With
    
    For Each oAPT In FOL.Items 'Search for existing meeting
                
        oAPT_DATE = Format(oAPT.Start, "MM-DD-YYYY")
        oAPT_TIME = TimeValue(oAPT.Start)
        
        If oAPT_DATE = ws.Cells(r, c - 3).Value And oAPT.Subject = ws.Cells(r, 1).Value And oAPT_TIME = ws.Cells(r, c - 2).Value Then
            check = True
        Else
        End If
    
    Next oAPT
    
    If check = False Then 'If no meeting already exist Then create new meeting
        Set oAPT = FOL.Items.Add(olAppointmentItem)
        With oAPT
            .Start = ws.Cells(r, c - 3).Value + ws.Cells(r, c - 2).Value
            .Duration = ws.Cells(r, c - 1).Value * 60
            .Subject = ws.Cells(r, 1).Value & " " & ws.Cells(1, c).Value
            .Body = "Project: " & ws.Cells(r, 1).Value & vbCrLf & "Location: " & ws.Cells(r, 2) & vbCrLf & "OASIS#: " & ws.Cells(r, 3) & vbCrLf & "Project Manager: " & ws.Cells(r, 5) & vbCrLf & "Distributor: " & ws.Cells(r, 8) & vbCrLf & "Assigned Technitian: " & ws.Cells(r, c - 5) & vbCrLf & "Date: " & ws.Cells(r, c - 3) & vbCrLf & "Start Time: " & Format(ws.Cells(r, c - 2), "h:mm am/pm") & vbCrLf & "Duration: " & ws.Cells(r, c - 1) & " Hour(s)"
            .Location = ws.Cells(r, 2).Value
            .Recipients.Add Cells(r, c - 4).Value
            .MeetingStatus = olMeeting
            .ReminderMinutesBeforeStart = 1440
            .Save
            .Send
        End With
        ws.Cells(r, c - 1).Locked = True
        ws.Cells(r, c - 2).Locked = True
        ws.Cells(r, c - 3).Locked = True
        ws.Cells(r, c - 5).Locked = True
    Else
    End If
    
    ws.Cells(r, 1).Locked = True
    ws.Cells(r, 2).Locked = True
    ws.Cells(r, 3).Locked = True
    
    ws.Protect "", True, True
    
End Sub

删除对 Outlook 库的所有 VBA 项目引用。

声明任何 Outlook 对象 As Object 而不是使用 Outlook 库类型名。使用 CreateObject() 代替 New

例如:

Dim oAPT As Outlook.AppointmentItem
Dim o As Outlook.Application
Set o = New Outlook.Application

将是

Dim oAPT As Object
Dim o As Object
Set o = CreateObject("Outlook.Application")

任何源自 Outlook 的常量,例如 olAppointmentItem 应在您的代码中声明为常量,或替换为它们的数值(您可以使用对象浏览器找到这些数值)