【发布时间】:2015-07-10 09:51:59
【问题描述】:
每天早上,我们都会收到一封自动发送的电子邮件,其中包含今天的定价。我在 Outlook 会话中设置了一个触发器,用于检查每封新电子邮件,如果有主题,则启动一个脚本。问题是,这个触发器有时只起作用。我让机器一直运行并且前景打开,但我发现触发器在一段时间后停止工作。通常我只需要重新启动 Outlook - 有时我只需要修改并重新保存代码。
有什么我可以更改以使此触发器更可靠的吗?
注意:我无权访问任务计划程序。
更新:我已经开始收到以下警报:
文件 C:\Users\me\AppData\Local\Microsoft\Outlook\Me@host.com.ost 正在使用中,无法访问。关闭任何正在使用的应用程序 这个文件,然后再试一次。您可能需要重新启动计算机。
点击 OK 不会中断脚本的运行 - 但它是否会暂停触发器向前推进?
这是脚本:
'Placed in Microsoft Outlook Objects > ThisOutlookSession
Private WithEvents olInboxItems As Items
Private Sub Application_Startup()
Set olInboxItems = Session.GetDefaultFolder(olFolderInbox).Items
End Sub
Private Sub olInboxItems_ItemAdd(ByVal Item As Object)
If TypeOf Item Is mailItem Then
Call handleMessage(Item.EntryID)
End If
End Sub
Public Function handleMessage(strMailItemID As String)
' This function takes an email ID and determines whether it's our pricing sheet email.
Dim mailItem As Outlook.mailItem
Dim path As String, targetSubj As String
Dim result As Boolean
result = False
targetSubj = "Today's Pricing"
Set mailItem = Application.Session.GetItemFromID(strMailItemID)
Debug.Print (mailItem.Subject) 'I use this to test if my script is working
'I find that it logs the subject and then simply cuts off.
If (Mid(mailItem.Subject, 1, Len(targetSubj)) = targetSubj) Then
Debug.Print ("Grabbed Target " & Date)
path = "C:\pathToPricing\Pricing.xls"
mailItem.Attachments.Item(1).SaveAsFile path
Call runExcelMacro("C:\pathToPricing\pricingAuto.xlsm", "processPricing")
result = True
End If
handleMessage = result
End Function
【问题讨论】:
标签: vba outlook triggers ms-office