在 outlook 中使用日历约会触发 VBA 宏

Use calendar appointment in outlook to trigger VBA macro

如何在 Outlook 中设置约会以使其通过约会提醒触发 VBA 宏? 在我的例子中,我希望 outlook 被安排在某个时间打开一个 excel 文件。

有一些示例,但 none 符合我的要求,因为大多数使用 Outlook 任务而不是约会。

例如: https://www.slipstick.com/developer/code-samples/running-outlook-macros-schedule/ 而这个

假设我们创建一个约会并将其命名为“脚本 运行”。

我们设置了应该 运行 的时间(您可以添加重复)并且不要忘记选择提醒!

同时创建一个类别并命名。

然后我使用修改后的代码版本并将其粘贴到“ThisOutlookSession”中:

要粘贴到“ThisOutlookSession”的代码

'The Private subs go in ThisOutlookSession
'declare this object withEvents displaying all the events
'might need to access library "Microsoft Excel 16.0 Object Library"

Private WithEvents olRemind As Outlook.Reminders

Private Sub Application_Reminder(ByVal Item As Object)

Set olRemind = Outlook.Reminders

If Item.MessageClass <> "IPM.Appointment" Then
    Exit Sub
End If

If Item.Categories <> "Run weekly script updates" Then 'Add another If statement to add additional appointments
    Exit Sub
End If

Call ExecuteFile ' call sub

End Sub

Private Sub olRemind_BeforeReminderShow(Cancel As Boolean)

'This is to dismiss the reminder

For Each objRem In olRemind
        If objRem.Caption = "Script Run" Then
            If objRem.IsVisible Then
                objRem.Dismiss
                Cancel = True
            End If
            Exit For
        End If
    Next objRem
End Sub

为了触发打开 excel 文件,我们使用位于“Module1”的子例程。 它看起来像这样:


版本 1:

Sub ExecuteFile()


Call Shell("G:\Till\Budget script.exe", vbNormalFocus) 'To call an external program

'Run Excel macro
Dim xlApp As Excel.Application
Dim xlBook As Workbook

Set xlApp = New Excel.Application
Set xlBook = xlApp.Workbooks.Open("G:\Till\Budget.xlsm") ' update with Excel name
xlApp.Visible = True

'// Run Macro in Excel_File
xlBook.Application.Run "Module1.CheckDates"

Set xlApp = Nothing
Set xlBook = Nothing

End Sub

版本 2 (late-binding):

当您的授权访问权限有限时,这对工作很有用。

Sub ExecuteFile()

Call Shell("G:\Till\Budget script.exe", vbNormalFocus) 'To call an external program

'Run Excel macro
Dim xlApp As Object
Dim xlBook As Workbook
Dim blnEXCEL As Boolean


'Establish an EXCEL application object, by Ken Snell
'https://social.msdn.microsoft.com/Forums/office/en-US/81d29bf1-524c-4303-8101-611cc30d739b/using-excel-objects-via-late-binding?forum=accessdev
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
      Set xlApp = CreateObject("Excel.Application")
      blnEXCEL = True
End If
Err.Clear
On Error GoTo 0


Set xlBook = xlApp.Workbooks.Open("G:\Till\Budget.xlsm") ' update with Excel name
xlApp.Visible = True

'// Run Macro in Excel_File
xlBook.Application.Run "Module1.CheckDates"

Set xlApp = Nothing
Set xlBook = Nothing

End Sub