-1

如何在 Outlook 中设置约会以使其通过约会提醒触发 VBA 宏?就我而言,我希望将 Outlook 安排在某个时间打开一个 Excel 文件。

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

例如: https ://www.slipstick.com/developer/code-samples/running-outlook-macros-schedule/ 和这个Outlook 和 Excel VBA 任务计划程序

4

1 回答 1

5

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

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

在此处输入图像描述

还要创建一个类别并为其命名。

在此处输入图像描述

然后我使用它粘贴到“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(后期绑定):

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

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
于 2020-09-23T20:00:37.977 回答