【问题标题】:Outlook Task Scheduler to Run Access Subs运行 Access Subs 的 Outlook 任务计划程序
【发布时间】:2014-10-22 18:37:39
【问题描述】:
我有运行一系列查询、形成表格并将这些表格发送给多个收件人的 Access 宏。这很好用,但我越来越多地被要求在一周中的某一天提供越来越多的报告。
我可以在 Outlook 中运行 VBA 以在某一天运行 Access 宏,而不是使用我的日历来提醒我打开 Access 并运行这些宏吗?
【问题讨论】:
标签:
vba
ms-access
outlook
【解决方案1】:
我可以在 Outlook 中运行 VBA 以在某一天运行 Access 宏,而不是使用我的日历来提醒我打开 Access 并运行这些宏吗?
您可以使用 .BAT 文件和 Windows 任务计划程序执行此操作,但您也可以使用 Outlook VBA 执行此操作。我有一些日历事件具有这个确切目的,执行代码并清除提醒。
首先,您需要添加一个事件处理程序,以便在设置提醒时触发。
Private WithEvents olRemind As Outlook.Reminders
然后您需要初始化WithEvents。每次发生提醒时我都会这样做,因为有时我会破坏代码或硬停止,这会丢失事件处理程序。
Private Sub Application_Reminder(ByVal Item As Object)
Set olRemind = Outlook.Reminders
End Sub
然后,您想要处理日历上的事件。我的设置方式是为要触发的事件添加特定类别。这有助于使我的日历更清晰。您可以通过多种方式做到这一点。
然后我也会关闭提醒,使其不显示。
'fire off automatic macros based on recurring reminders
Private Sub olRemind_BeforeReminderShow(Cancel As Boolean)
Dim objRem As Reminder
For Each objRem In olRemind
'get categories
Dim rmdrCategories As String
rmdrCategories = objRem.Item.categories
'call the macro based on category
If InStr(rmdrCategories, "whateverYouWantTheReminder") > 0 Then
'only run if this reminder is visible
If objRem.IsVisible Then
'This code is specific to whatever macro you want to run
Dim mydb As Object
Set mydb = GetObject("...pathToDatabase.mdb")
mydb.Application.Run "YourMacroName"
mydb.Application.Quit
Set mydb = Nothing
objRem.Dismiss
Cancel = True
End If
End If
Next objRem
End Sub