0

I'm working on an auto reply in Outlook 2010 using a VBA script so that whenever someone emails me, there's an auto response that responds back with my current unread email count along with the date and time of the oldest email.

In regards to Code this is what I have so far:

Sub AutoResponse(objmsg As Outlook.MailItem)

    Dim objReply As MailItem
    Dim omsgItem As Outlook.MailItem
    Dim inbox As MAPIFolder
    Set inbox = Application.GetNamespace("MAPI"). _
    GetDefaultFolder(olFolderInbox)


    Set objReply = objmsg.Reply


    objReply.Subject = "Re: " & objReply.Subject
    objReply.HTMLBody = "Hello," _
    & "<br>" _
    & "<br>" _
    & "<br>" _
    & "Thank you for your email. This is to verify that I have received your email. I currently have " & inbox.UnReadItemCount & " unread emails and the oldest email is (Code goes here) in my inbox and will be reading emails in a first in first out fashion. If the matter is urgent and cannot wait please call me directly" _
    & "<br>" _
    & "<br>" _
    & "Regards, Mike" _
    & "<br>" _
    objReply.Send
    Set objReply = Nothing

End Sub

The exact wording will be different but you should get the idea.

I'm extremely new to this and have been able to piece together a bit of code from Google and previous knowledge when developing a different app in .Net.

Any help would be greatly appreciated.

3
  • 1
    Where is your question?
    – DavidPostill
    Dec 15, 2014 at 18:42
  • Anybody know what the code is for pulling the date and time of the last unread email in your inbox and displaying that in the body of an email.
    – Jae
    Dec 15, 2014 at 18:57
  • Code discussion is more of a StackOverflow question. I'll flag it for migration over there hopefully.
    – Sun
    Dec 15, 2014 at 22:07

1 Answer 1

2
Const olFolderInbox = 6
Dim mapiNamespace As Outlook.NameSpace
Dim inboxFolder As Outlook.Folder
Dim unreadItems As Outlook.Items

Set mapiNamespace = Application.GetNamespace("MAPI")
Set inboxFolder = mapiNamespace.GetDefaultFolder(olFolderInbox)
Set unreadItems = inboxFolder.Items.Restrict("[Unread] = True")
unreadItems.Sort "[ReceivedTime]", False

The above Code filters only the unread mails from your inbox and sorts them by received time. If you got that you can easily access the oldest one with:

unreadItems.GetFirst().ReceivedTime

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .