2016-04-03 103 views
0

我需要创建一个VBA脚本,该脚本在启动时在Outlook中运行,然后在两分钟之后运行。这里所说:VBA运行代码在启动时和开启时间间隔

Private Sub Application_Startup() 
Call doThis 
End Sub 

Private Sub checkEmail(Item As Outlook.MailItem) 
    'lots of code here 
End Sub 

Sub doThis() 

Dim myInbox     As Outlook.Folder 
Dim ToPrint     As Outlook.Folder 
Dim myNameSpace    As NameSpace 
Dim objItems    As Outlook.Items 
Dim objItem     As Outlook.MailItem 

Set myNameSpace = Application.GetNamespace("MAPI") 
Set ToPrint = myNameSpace.GetDefaultFolder(olFolderInbox).Folders("ToPrint") 
Set objItems = ToPrint.Items 

For Each objItem In objItems 
Call checkEmail(objItem) 
Next 

Application.Ontime Now + TimeValue("00:02:00"), "doThis" 

Set myInbox = Nothing 
Set ToPrint = Nothing 
Set myNameSpace = Nothing 
Set objItem = Nothing 
Set objItems = Nothing 

End Sub 

因此,代码运行的很好,除非我得到这一行:

Application.Ontime Now + TimeValue("00:02:00"), "doThis" 

在这一点上,我得到运行时错误438:“对象不支持此财产或方法“。

任何人有想法吗?我是VBA的新手,但在过去几周里我学到了很多东西。我将不胜感激。

回答

1

Outlook不支持Application.RunApplication.OnTime方法。

但是,您可以使用支持它的Object模型(如Excel)中的OnTime方法。也就是说,您可以从Excel中自动执行Outlook,但根据您希望在Outlook中实现的目标,您可能会被Outlook Object Model Guard阻止。

+0

谢谢!我可能会切换到一个脚本,它将运行在所有收到的电子邮件中,这总是一个(稍微差一些)的选项。 – richardsonralph