Outlook VBA for marking a new mail as a task before sending

◇◆丶佛笑我妖孽 提交于 2019-12-13 02:23:55

问题


I have tried this:

Sub SendAwaitingResponse()
  Dim mail As MailItem
  Set mail = Outlook.Application.ActiveInspector.CurrentItem
  mail.MarkAsTask (olMarkToday)
  mail.Categories = "Awaiting Response"
  mail.Send
End Sub

with the following error:

Draft Items cannot be marked. MarkAsTask is only valid on items that have been sent or received.

It is possible to click "Follow up" in the UI, and then the mail will be marked as task after sent. I see no way to do it programmatically.


回答1:


You would need to listed to the Items.ItemAdd even on the Sent Items folder and call MarkAsTask on the message passed to the event handler.




回答2:


Create a macro associated to a button "Send Awaiting Response", in order to mark sent emails with a custom property:

Sub SendAwaitingResponse()
    Dim Mail As MailItem
    Set Mail = Outlook.Application.ActiveInspector.CurrentItem
    Dim Property As UserProperty
    Set Property = Mail.UserProperties.Add("FlagAwaitingResponse", olYesNo)
    Property.Value = True
    Mail.Send
End Sub

Inside the ThisOutlookSession object, subscribe to items added to the Sent Items folder. Items marked with the custom property will be marked as an Outlook task.

Private WithEvents Items As Outlook.Items

Private Sub Application_Startup()
    Dim SentItems As Folder
    Set SentItems = Outlook.Application.GetNamespace("MAPI").GetDefaultFolder(olFolderSentMail)
    Set Items = SentItems.Items
End Sub

Private Sub Items_ItemAdd(ByVal Item As Object)
    Dim Mail As MailItem
    Set Mail = Item

    Set Property = Mail.UserProperties("FlagAwaitingResponse")
    If Property Is Nothing Then Exit Sub

    Mail.Categories = "Awaiting Response"
    Mail.MarkAsTask (olMarkToday)
    Mail.Save
End Sub

Don't forget to enable Outlook macros for this to work.



来源:https://stackoverflow.com/questions/19415104/outlook-vba-for-marking-a-new-mail-as-a-task-before-sending

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!