Share via

How to trigger a vba script, if an email arrives, is send or an email is moved from one folder to another folder (via the outlook GUI)?

Anonymous
2025-02-23T21:49:45+00:00

I am using VBA in combination with a database, to manage sort, move and delete outlook emails.

To do that in an efficient way I need triggers related to the following events:

a) arrival of new mail

b) a mail has been send via the outlook GUI

c) a mail has been moved or deleted via the outlook GUI

Next to that I would love to have:

d) a command which select all emails new or moved after a certain date

(in order to efficiently update /synchronize my DB)

"a" is not a problem you can define a outlook rule which starts a (vba) script if a mail arrives

"b" I did expect to have the same rule script option for send mails, but strange enough that option is NOT available

"c" the is an mail property LastModificationTime and I hoped that an email folder move would change that time. It is not :(

"d" no idea given the test result related to LastModificationTime, I have no Idea for a solution

So the very sad result of my testing and searching up to now is that for 3 out of 4 named items, I do not have (a performance effective) solution !! :( :(

So I really hope someone has a good option for one or more of the mentioned issues b) , c) and d)

Sincerely,

Louis

For info:

  • I am using Outlook 365 on WIndows11 64bit pro
  • My tool is using outlook VBA / msaccess VBA as frontend and MsAccess or MySQL as database.
Outlook | Windows | Classic Outlook for Windows | For business

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

5 answers

Sort by: Most helpful
  1. Anonymous
    2025-02-24T13:49:54+00:00

    I was very very disappointment that I could only trigger a script for incoming mails and NOT for sent mails or moved mails via the GUI.

    So I decided to investigate if it was perhaps possible to create the desired rules via VBA using the outlook.rule object. NoWay :(

    I did have a look here

    'https://learn.microsoft.com/en-us/office/vba/api/outlook.olruleactiontype

    'https://learn.microsoft.com/en-us/office/vba/outlook/how-to/rules/specifying-rule-actions

    Result:

    'Start a script olRuleActionRunScript No (programmatically) Yes (Receive rules) No (Send rules)

    '=> No Change to define a send rule which triggers a script neither via the wizard or via VBA

    'olRuleActionNewItemAlert Yes Yes No (Send rules) '=> No change to have an Alert in case of a new message 'olRuleActionMoveToFolder Yes Yes No (Send rules) '=> This is an action not an alert (and not related to send)

    So also this route seems to be dead

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2025-02-24T10:34:42+00:00

    Perhaps you can help me a bit. I did take another look at the link you send and are confused.

    The sub below seems to be a sub to send a mail. An action done by the user in my scenario

    Sub SendMyMail() 
     Set myItem = Outlook.CreateItem(olMailItem) 
     myItem.To = "Dan Wilson" 
     myItem.Subject = "Data files information" 
     myItem.Send 
    End Sub
    
    This sub feels like a sub to put a send mail action in an event list
    to send the mail at 2/2/2003 
    Private Sub myItem_Send(Cancel As Boolean) 
     myItem.ExpiryTime = #2/2/2003 4:00:00 PM# 
    End Sub
    
    So I am confused ..
    
    Related to the lastupdate time, I did test that (I hope I did not make mistakes), but moving an item to another folder seems not to change the 
    update time :( . I just home that I am mistaken. 
    Dim LastUpDate As Date
    LastUpDate = mailobject.LastModificationTime
    Would not help any way, since that is asking info from an object, which I was searching and not yet have located
    
    Tja the restrict filter .... I did some testing with that one .... and IMHO it is a drama for a couple of reasons:
    - a date will never match since the outlook internal timestamp has an higher precision than the date format,
    so it will never match. A work around is to search for a value between two timestamps
    - searching for a text in e.g. subject is impossible since the query does not handle modern (not basic ascii) in correct 
    and you can not escape every potential special character. Not if you know the search string in advance and not at
    all in case the search sting is unkown is inside a  string variable
    - the used date sting format ... lets say I do not like it
    But I worked around that by using the restrict function as minimal as possible and not for strings
    

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2025-02-24T09:28:52+00:00
    1. The links I provided are for Events (Outlook calls your code when something happens) not Methods (your code tells Outlook to do something).
    2. I'm not sure why LastModificationTime is not what you want. See:

    Meanings of e-mail timestamps in MS Outlook VBA

    1. You can use Outlook restrict/filter/sort features to work with a group of items. See:

    extract the last updated timestamp from outlook item

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2025-02-24T06:37:02+00:00

    Not sure I had a look at the link you provided on question b) solves my problem. I can trigger all kind of actions from VBA including sending or moving mails. That is not the problem. The problem is that I need triggers for events started by the user. Like sending a mail or moving or deleting mails.

    Related to c). No I do not know which email (or folder) the user will be moving. So I need a trigger (including the mail key / item reference) to sync my database and perhaps starting some action.

    At this moment the only option I have is to walk along all outlook mails to see what has changed, which is of course terribly inefficient.

    Note that the it is most likely but not guaranteed that mails have been moved from the inbox of send item box, but you can not be sure about that.

    Was this answer helpful?

    0 comments No comments
  5. Anonymous
    2025-02-23T23:43:11+00:00

    b)
    https://learn.microsoft.com/en-us/office/vba/api/outlook.mailitem.send(even) **** https://learn.microsoft.com/en-us/office/vba/api/outlook.items.itemadd

    c) Assuming your know which target folders you want to monitor for items "moved" into them. Items.Item.Add Event. Same for the Delete Items folder.

    Was this answer helpful?

    0 comments No comments