• Outlook User
  • New Outlook app
  • Outlook.com
  • Outlook Mac
  • Outlook & iCloud
  • Developer
  • Microsoft 365 Admin
    • Common Problems
    • Microsoft 365
    • Outlook BCM
    • Utilities & Addins

Outlook's Rules and Alerts: Run a Script

Slipstick Systems

› Outlook › Rules, Filters & Views › Outlook’s Rules and Alerts: Run a Script

Last reviewed on July 14, 2021     155 Comments

A security update disabled the Run a script option in the rules wizard in Outlook 2010 and all newer Outlook versions. See Run-a-Script Rules Missing in Outlook for more information and the registry key to fix restore it.

Using the "Rules and Alerts" option to "run a script", the list of scripts to run is empty. What makes a script appear in the list?

The argument must by type MailItem or MeetingItem for the subroutine to be available in the Rules Wizard in Outlook 2007. In Outlook 2010 and 2013 (and up), PostItem also works.

You need to avoid processing messages using more than one rule when you are using a run a script rule. Run a script rules should be placed at the top of the rules list and include the "stop processing" action.
All actions should be in the script - don't combine actions in the rule with the run a script rule. Include all of the actions you want to take on the items that meet the rule's conditions in the VBA procedure.

Something like this:

Public Sub ShowMessage(Item As Outlook.MailItem)

'code

End Sub

Using VBA Editor

Open Outlook's VBA editor by pressing Alt+F11 and expand Microsoft Office Outlook Objects then double click on ThisOutlookSession.

Script in the VBA Editor

You'll type or paste the code into the module, then create the rule with the 'run script' Action and select this script

Create a Run a Script Rule

After the script is written, you need to add it to a rule.

If you have Outlook 2016 and run a script action is missing, see "Run-a-Script Rules Missing in Outlook" for the fix.

  1. Open Rules Wizard. In Outlook 2010 and 2013, it's on Outlook's Home ribbon,
    Rules > Manage Rules & Alerts. Look on the Tools menu in older versions.
    Open Rules Wizard
  2. Click New Rule.
  3. Select Apply Rule on messages I receive and click Next.
  4. Select your conditions and click Next.
  5. Select Run a script action (near the bottom).
    The finished run a script rule
  6. Click on a script.
    Select the script
  7. Select your script, click OK.
  8. Click Next then finish the rule.

For best results, all actions should be in the script.

More Information

  • Filter mail when you are not the only recipient
  • Process Mail that was Auto Forwarded by a Rule
  • Run a Script: Send Autoreply with Date
  • Filter messages containing spam or other words
  • Creating an AND rule in Outlook Rules
  • Search PDF Attachments and Forward
  • Run-a-Script Rules Missing in Outlook
  • Blocking Mail From New Top-Level Domains
  • How to Process Mail After Business Hours
  • Create Appointment From Email Automatically
  • Open Hyperlinks in an Outlook Email Message
  • Talking Reminders
  • Receive a Reminder When a Message Doesn't Arrive?
  • Delegates, Meeting Requests, and Rules
  • Run Rules Now using a Macro
  • Automatically Add a Category to Accepted Meetings
  • Save and Rename Outlook Email Attachments
  • Use a Rule to delete older messages as new ones arrive
  • Convert RTF Messages to Plain Text Format
  • Outlook AutoReplies: One Script, Many Responses
  • Use VBA to move messages with attachments
  • Save all incoming messages to the hard drive
  • Run a script rule: Reply to a message
  • Sort messages by Sender domain
  • Use a Macro to Copy Data in an Email to Excel

Additional Information:

  • How to create a script for the Rules Wizard in Outlook (MSKB)
  • Retrieving Internet Headers Using VBA in Outlook 2007/2010
Outlook's Rules and Alerts: Run a Script was last modified: July 14th, 2021 by Diane Poremsky
Post Views: 70

Related Posts:

  • Disable New Mail Notifications for Some Accounts
  • out of office rules
    Show a Desktop Alert when Rules move messages
  • A simple run a rule script marks Outlook messages read when the messag
    Use a run a script rule to mark messages read
  • Creating an AND rule in Outlook Rules

About Diane Poremsky

A Microsoft Outlook Most Valuable Professional (MVP) since 1999, Diane is the author of several books, including Outlook 2013 Absolute Beginners Book. She also created video training CDs and online training classes for Microsoft Outlook. You can find her helping people online in Outlook Forums as well as in the Microsoft Answers and TechNet forums.

Comments

  1. Mario says

    July 31, 2023 at 10:33 am

    Hello Diane, greetings from Brazil!

    We have a doubt in outlook and needs your help:

    In my company , we need automate some tasks in Outlook. For example, when we apply some tag in email, we want these email tagged be moved to folder automatically.

    I know that outlook web and outlook desktop have rules that i can automate many tasks, like move email to folder from tag, sender, subject, etc, that rules works when emails are sended and received.

    I found these manual task in Outlook desktop that move email to folder after i tagged that email manually. In these task, we need click on button "Run Rules Now" manually:

    https://filestore.community.support.microsoft.com/api/images/d58b5400-c18c-426d-84fa-0a934716c28e?upload=true

    https://filestore.community.support.microsoft.com/api/images/b7a0c5d8-e7e9-41f6-a131-b074cd359483?upload=true

    however, in my company we need automate tasks after email arrive in inbox.

    This is my doubt: is possible create automatic task for when i apply tag in email received, these email be move to folder automatically? there are script or macro related?

    Thanks a lot!!

    Mario

    Reply
  2. mishu36 says

    August 10, 2021 at 6:41 am

    hi, i am trying to use "run a script" but in my "Outlook 2019", but that option is missing.

    i tried to use the registry hack of "EnableUnsafeClientMailRules" but it also not working. i want to use a way around but for that i need to execute VBA Macro before outlook rules can be run. Can you please guide me on this.

    here is my action plan sequence:

    1. run vba code
    2. run outlook rules
    Reply
    • Diane Poremsky says

      November 2, 2021 at 4:12 pm

      Did you restart Outlook after setting the key?

      If you ended the registry yourself, try using the exported key from this website.

      Reply
  3. Rakesh Kumar says

    January 31, 2021 at 12:56 am

    Hello Brother,
    My script is not showing in Run a Script.

    Reply
    • Diane Poremsky says

      February 2, 2021 at 12:52 am

      Did you put it in ThisOutlookSession? Try putting the script in a new module instead.

      The macro 'name' needs to be in this format:
      Public Sub ShowMessage(Item As Outlook.MailItem)

      Reply
  4. josue aguirre says

    August 4, 2020 at 1:24 pm

    Hello, can you please help me with a problem?
     
    I have a rule in Outlook that runs a script, but it suddenly doesn't work anymore. And when I want to change this rule or create a new one I get a message:
    "This rule will only be executed when you check your email ..."

    Reply
  5. swinn says

    June 11, 2020 at 11:44 am

    Discovered the hard way that if the Sub you are trying to invoke with an Outlook Rule has an Optional parameter, the Rule Editor won't display it for you to select.

    Reply
  6. egenc says

    March 27, 2020 at 2:30 am

    I have added script but can't see it in rule page.

    Reply
    • Diane Poremsky says

      June 1, 2020 at 11:14 pm

      The script needs to be named in this format, preferably in a module (not thisoutlooksession) - you don't need to call the mail object Item, but you do need it in the ().
      Public Sub ShowMessage(Item As Outlook.MailItem)

      Reply
      • Isaac says

        July 29, 2020 at 5:45 pm

        It's exactly the opposite. It MUST be in the thisoutlooksession, and CAN'T be in a regular module.

      • Diane Poremsky says

        July 29, 2020 at 9:15 pm

        No, it will work in either, but it is recommended to put it in a module. I've had instances where it was not 'seen' if in thisoutlooksession, but none where it was not seen in a module.

  7. ardi says

    December 16, 2019 at 2:43 am

    how to use in outlook 2016

    Reply
  8. Michael says

    November 21, 2019 at 3:53 pm

    I'm trying to use the "Send a message to someone when a reminder fires" macro from this page: https://www.slipstick.com/developer/send-email-outlook-reminders-fires/, but it seems like the parameters of that code aren't compatible with what's required for the Rules Wizard to recognize it as a script.

    Reply
  9. Angelo says

    July 8, 2019 at 10:02 am

    Hi,
    Thank you for the article.

    Reply
  10. Yuvaraj says

    June 6, 2019 at 11:51 am

    Hi,

    I have tried with the options as suggested, but while i tried to set the rules in the action pane the rule "Run a Script", I cant able to find my macro name while i clicked on the script underlined one. Could you pls suggest me why can't able to find the macro name. Please?

    Thanks,
    Yuvaraj

    Reply
    • Diane Poremsky says

      June 6, 2019 at 12:34 pm

      What is the macro name? It needs to be formatted like this
      Public Sub ShowMessage(Item As Outlook.MailItem)

      Is the macro in thisoutlooksession or a module? It should be visible in either, but sometimes won't work in thisoutlooksession.

      Reply
      • genc says

        March 27, 2020 at 2:32 am

        how to give a name like this? From where

  11. Bob says

    May 1, 2018 at 2:31 pm

    I have set up a forward script on a co-workers computer, it worked for about 4 days and then it quit working. when I investigated the script was gone. the next week it did the same thing. I have the exact same script and rule set up on my computer and have never had this issue.

    Reply
  12. Rohit says

    March 12, 2018 at 11:28 am

    Hello,

    The article was really impressive.
    I followed the steps for running a batch file. However, when I was testing that by sending a mail from myself to me to check if the batch file gets triggered, I see that the batch file is not triggered.
    I am not sure why it is not triggering the batch file.
    Could you please help.

    Thanks.

    Reply
    • Diane Poremsky says

      March 15, 2018 at 12:26 am

      What batch file are you running? (There isn't one on this page.)

      Reply
    • Diane Poremsky says

      March 15, 2018 at 12:28 am

      BTW, if the rule isn't running, you may need to use an ItemAdd macro that watches the folder for new items.

      Reply
  13. Erfan says

    September 30, 2017 at 8:57 am

    I'm new in VBA, I have some folder in my outlook and I need move the incoming message to the related folder, the related folder can search by a key in the subject of the new email and I don't have any idea to how I can search by a key and retrieve related folder.

    I develop a script for outlook as below for getting the key in subject of new message

    Sub CustomMailMessageRule(Item As Outlook.MailItem)
    Dim strTicket, strSubject As String
    Dim strFolder As String
    strTicket = "None"
    strSubject = Item.Subject
    If InStr(1, strSubject, "#-") > 0 Then
    strSubject = Mid(strSubject, InStr(strSubject, "#-") + 2)
    If InStr(strSubject, " ") > 0 Then
    strTicket = Left(strSubject, InStr(strSubject, " ") - 1)
    End If
    End If
    and the unknown part is below part which I need to search whole my folders by related key and retrieve the related folder

    strFolder = "???"
    and finally, move the incoming message to the related folder by below code

    If InStr(strFolder) > 0 Then
    Item.Move Session.GetDefaultFolder(olFolderInbox).folders(strFolder)

    MsgBox "Your New Message has been moved to related folder "
    End Sub
    I'm really appreciated if someone can help me and let me know how can I do that and please correct me if there is any mistake in my code.

    Reply
    • Diane Poremsky says

      October 2, 2017 at 1:06 am

      you need to use an array - see https://www.slipstick.com/developer/using-arrays-outlook-macros/ for an example

      Reply
  14. Steven Vincent says

    August 31, 2017 at 4:50 pm

    Hi Diane,

    I do not have the option to run a script when I go to create new rule. Is there a way to activate this?

    Reply
    • Diane Poremsky says

      August 31, 2017 at 9:16 pm

      Yes, you need to set a registry key - it was removed from outlook 2013 and 2016 in in the June security update.
      See https://www.slipstick.com/outlook/rules/outlook-2016-run-a-script-rules/ for the key and a ready to use reg file.

      Reply
  15. Loren Pearson says

    August 21, 2017 at 10:47 am

    Good morning Diane,

    Great tutorials and code examples for exploring Outlook VBA possibilities.

    My issue stems from the removal of Run a Script, in that my organization has vetoed my request (and that of others) to have the Registry change implemented as an exception. They have left the Macros run through Run a Script as too dangerous, and referred me to NewMailItems as an alternative.
    However, I have found that it is not as effective.

    My issue is that I am unable to pull in those that populate my inbox when I am logged off of my machine (overnights, weekends, etc.). There are often hundreds of reports in my inbox for post processing (appending to spreadsheets, saving into shared folders, etc.) after a weekend. They run fine when I am logged in, and when I was not, the Run a Script function used to capture all of as soon as I logged in, but now they are all unprocessed.

    I can forward them to myself, and they get captured that way, but there must be automated method to do this. What would be your recommendation to process these files already in the inbox, as a rule would have? Is there an ApplicationStartup tool to pull in these files. Thank you.

    Reply
    • Diane Poremsky says

      August 31, 2017 at 9:19 pm

      I use itemadd - that will pick up new items as they are added to the folder. See https://www.slipstick.com/developer/itemadd-macro/ for more information.

      Reply
  16. Brian Vogel says

    August 6, 2017 at 11:57 pm

    Hello Diane,

    I'm here to bother you on this page because it's the "next step" and I'm having a bit of trouble.

    When I have finished with a test script, and go through the steps above to create a rule to run it, no scripts show up in the list.

    What must I do "up front" after the script is tested so that it will show up in that list?

    Thanks again for your assistance.

    Reply
    • Diane Poremsky says

      August 7, 2017 at 10:56 am

      What did you name the script? The name line should be in this format:
      Public Sub YourScriptName(Item As Outlook.MailItem)

      if you don't have the Item As Outlook.MailItem in the (), it won't be seen by the rule.

      if you mean, you can't see the rule action for run a script, see https://www.slipstick.com/outlook/rules/outlook-2016-run-a-script-rules/ - both 2013 and 2016 had run a script disabled by a recent security patch.

      Reply
      • Brian Vogel says

        August 7, 2017 at 1:50 pm

        Diane,

        Thanks again. I had failed to declare the parameter and, without it, that script will not appear in the list of scripts since *something* has to be passed to a script invoked by a rule. Or at least it looks that way because it showed up without issue once I'd corrected that oversight.

        I have Outlook 2010, but the person I'm assisting has Outlook 2016, and I had already noticed your warning at the top of this page and on at least one other page at slipstick.

        Brian

  17. Deepak says

    June 15, 2017 at 7:37 am

    How i can get E-mail fields as input for external command.
    Example :
    I would like to pass subject and msg to below parameters.

    Object =
    Message =

    Reply
    • Diane Poremsky says

      June 15, 2017 at 9:23 am

      you'd use the object name and field name - in this example, I'll assume item is representing the outlook.mailitem:
      item.subject

      I'm not sure what you mean by msg - is that is the mailitem, how you reference it depends on how you are accessing it. In a run a script macro, you'd put it in the () in trhe macro name - sub macroname(item as outlook.mailitem)

      Reply
      • deepak says

        July 3, 2017 at 4:57 am

        Thanks for reply , I have one more query ,
        A. Do we need to keep open outlook client on system always to run the script ?

      • Diane Poremsky says

        July 3, 2017 at 10:10 am

        Yes, you do.

  18. Mike petrozelli says

    June 2, 2017 at 12:05 pm

    Hi Diane, I was tasked with a gentleman's request to extract an attachment from a specific email user and subject line and move it to a network folder so that others can view it without him having to constantly move it. I receive the email but don't see the file moved to the location:

    Private Sub Application_Startup()
    Dim olApp As Outlook.Application
    Dim objNS As Outlook.NameSpace
    Set olApp = Outlook.Application
    Set objNS = olApp.GetNamespace("MAPI")
    Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
    End Sub

    Private Sub Items_ItemAdd(ByVal item As Object)

    On Error GoTo ErrorHandler

    'Only act if it's a MailItem
    Dim Msg As Outlook.MailItem
    If TypeName(item) = "MailItem" Then
    Set Msg = item

    'Change variables to match need. Comment or delete any part unnecessary.
    If (Msg.SenderName = "Mike Petrozelli") And _
    (Msg.Subject = "Test") And _
    (Msg.Attachments.Count >= 1) Then

    'Set folder to save in.
    Dim olDestFldr As Outlook.MAPIFolder
    Dim myAttachments As Outlook.Attachments
    Dim Att As String

    'location to save in. Can be root drive or mapped network drive.
    Const attPath As String = "\servernametest"

    ' save attachment
    Set myAttachments = item.Attachments
    Att = myAttachments.item(1).DisplayName
    myAttachments.item(1).SaveAsFile attPath & Att

    ' mark as read
    Msg.UnRead = False
    End If
    End If

    ProgramExit:
    Exit Sub

    ErrorHandler:
    MsgBox Err.Number & " - " & Err.Description
    Resume ProgramExit
    Sub myRuleMacro(item As Outlook.MailItem)
    End Sub
    End Sub
    Sub Test1()

    End Sub

    Anything I could be missing code-wise??

    Reply
    • Diane Poremsky says

      June 2, 2017 at 10:11 pm

      Did you change macro security and restart outlook?
      add msgbox "Working" after the if.. thens - this will tell you where it is failing. Stepping though the code and watching which lies it skips could be helpful too.

      I'm assuming that the server path is correct and complete and the slashes were removed by my website software.

      Reply
  19. Eitan Orgil says

    May 3, 2017 at 7:00 am

    Hi Diane,
    I use Office 2007, and I have a script running in outlook that will extract only
    numbers from a mail, then it will update an Excel with that data and then
    send an email with a picture of a chart.
    I configured a Rule that runs when mail from A sender arrive, set it up to
    run the script.
    The problem is, when Rules and Alerts starts, the mail is not in the Inbox yet,
    so the script cannot find the mail, and I get the MsgBox "There is no mail".
    Once the Script exits and the Rule ends, the mail is in the Inbox, so now
    when I run the rule manually it runs with no problem.
    I added the "Stop Processing more rules" but it did not change the status,
    In the rules and Alerts it looks like it first run the script then stop processing
    more rules.
    What am I doing wrong?
    appreciate your help

    attaching the code.

    Reply
    • Diane Poremsky says

      May 6, 2017 at 10:27 pm

      you are getting that message with a run a script rule? I'm guessing its something in the script. can you post it?

      Reply
      • Eitan Orgil says

        May 7, 2017 at 6:29 am

        Hi Diane,

        ProcessMail.txt is the script.
        and the Pic is my Rules and Alerts screen shot.

        When the mail I want to process arrives, Rules and Alerts pics it up,
        at that moment I cannot see the mail in the Inbox,
        This is why I added the If Then Else section, When I get the message
        "There is no mail" the mail I want to process is no in the Inbox,
        only after I press the OK on the message box, I can see the mail,
        Unread, in the Inbox, then I run the Rule manually (Run Rules Now)
        and the Script does what I expect it to do.
        My script looks for the mail in the Inbox.

      • Diane Poremsky says

        May 7, 2017 at 11:17 am

        with run a script rules (or itemadd macros), you don't need to search for the message - the rule picks up the message - the script moves it and does whatever.

      • Eitan Orgil says

        May 8, 2017 at 5:50 am

        Hi Diane,

        OK, thanks for the clarification.
        Now, how do I handle the message? The Rules and Alerts Identified it
        as "Mail From", how do I move it? send it's content to the Function
        that extracts the Numbers from it? basically, what is it's "Name"?

      • Diane Poremsky says

        May 8, 2017 at 8:27 am

        when the rule picks it up, it sends to it to the script as Item or whatever word you use for mailitem in the macro -
        Public Sub ShowMessage(Item As Outlook.MailItem)

        that is the same as using this for the selected item - but the rule has already selected it.
        Set Item = objApp.ActiveExplorer.Selection.Item(1)

      • Eitan Orgil says

        May 9, 2017 at 6:18 am

        Hi Diane,

        Grate, this solved it and even simplified my code

        Thank you for your time

  20. Brenda Gorham says

    March 14, 2017 at 9:13 am

    Diane, Let me first state, I am a true beginner at VBA. My administration has requested a better means to identify reports coming through their Outlook daily. Currently, we use a product called Interbit as our report distribution software. Love the product, but unfortunately it does not allow me to identify the name of the attachment in the subject line. User's only see the subject line, unless they open each attachment.

    Could you direct me to the best option or help documentation on this subject; one for beginners, such as myself?

    I have goggled a lot regarding the creation of a VB macro and then applying a Run script rule. This sounds more like I would need to apply this to each recipient. Is there a method to apply a rule to the sender?

    Reply
    • Diane Poremsky says

      March 16, 2017 at 10:14 am

      do you need to run it on your computer only or on everyone's computer? Will it need to run when your computer is not turned on? While you can set macros up on a lot of computers, it's requires some effort to install them; and they only run when Outlook is running.

      That said, getting the names of the attachments and adding them to the subject isn't a big deal - it's actually pretty easy (this code sample is from the macro at Reply with the Attachment name):
      For Each oAtt In Item.Attachments
      strAtt = strAtt & "; " & oAtt.FileName
      Next oAtt
      item.subject = item.subject & " " & stratt

      Reply
  21. Mayank Manchanda says

    February 8, 2017 at 10:24 pm

    Hi Diane,

    I need to do something similar but with a different criteria. Are you able to help me with that?

    I receive inquiries through a realestate portal and I want to capture the contact details from the body of the email and export it to excel to avoid manual handling. Format of the email is same all the times.

    Can this be done through a macro and exported to excel?

    Reply
    • Diane Poremsky says

      February 9, 2017 at 12:58 pm

      it can be done using a macro - i have an example here - https://www.slipstick.com/developer/regex-parse-message-text/

      Reply
  22. Benoit Rompre says

    January 16, 2017 at 9:28 pm

    Hi,
    We have some script that are working with run script rule in outlook 2013 and older. Since upgrading and updated Outlook 2016 we do not have any "Run a script rule". All are rules are now in error. We are still able to open and run the macros selecting an email.

    Reply
    • Diane Poremsky says

      February 5, 2017 at 10:27 pm

      it's a new security feature (makes no sense to me as VBA in general would be equally secure or insecure) - you need to set a registry key to enable run a script.
      https://www.slipstick.com/outlook/rules/outlook-2016-run-a-script-rules/

      Reply
  23. Paul says

    November 2, 2016 at 6:13 pm

    Hi Diane--

    My VBA Macro works manually however if does not run as a Macro. I feel I have followed you excellent instructions but I must be missing something. I am trying to have a rule that once X email arrive on this client only run macro to copy email to a flat file. Thanks in advance.

    Reply
    • Diane Poremsky says

      February 5, 2017 at 10:35 pm

      Run a script macros can only run from rules (or if called from other macros) - they can't be run manually. It needs to be named
      Public Sub macroname(Item As Outlook.MailItem)

      Reply
  24. Herbert says

    October 25, 2016 at 1:45 am

    Hi Diane,

    you say, that under OL 2013 I should be able to process PostItem as well. I attempt to do that for incoming RSS posts. However, the rules wizard does not list procedures with this argument type, hence I can not select them. What am I doing wrong?

    Thanks in advance,
    Herbert

    Reply
    • Diane Poremsky says

      February 5, 2017 at 10:31 pm

      You would use the condition 'from any rss feed' (along with any other conditions) and then choose run a script rule on the actions screen.

      Reply
  25. Kruti says

    September 28, 2016 at 3:12 am

    I need to run a selenium script of which I have a .bat file
    I have no knowledge of VBA
    How do I write code to run my .bat file through outlook.

    Reply
    • Diane Poremsky says

      February 5, 2017 at 10:09 pm

      This article tells how to call programs from a macro - for a bat file, you need to use Shell (strURL), vbNormalFocus in the code.

      Reply
  26. Edward McArdle says

    August 9, 2016 at 3:08 pm

    Hi Diane

    Wondering if you had a chance to review my query?

    Regards
    Ed

    Reply
    • Diane Poremsky says

      August 10, 2016 at 1:09 am

      No. :)

      Reply
  27. Ed McArdle says

    August 8, 2016 at 5:10 am

    Hi,

    For the run a script rule, how do you get this to work in a shared mailbox and also, how do you alter this code so that if the ID already exists on the subject line it will not add a new one. I like to use this unique id to determine number of touch points to an email thread so therefore don't want it to create a new one each time.

    Many Thanks,
    Ed

    Reply
    • Diane Poremsky says

      August 10, 2016 at 1:14 am

      A run a script rule won't run on a shared mailbox - it needs to be opened in outlook as an account. You could use an itemadd macro and watch the folder for new items. Which macro are you using? You need to look for the code - the easiest way is using regex unless it's wrapped in [] or something to identify where it begins or ends. Then you can use instr, left or right functions.

      Reply
  28. Narendra says

    July 26, 2016 at 2:11 pm

    Hi Diane Poremsky,

    Hope your fine.

    I need a small help from your side, as I found an Excel Macro in Google but it's not serving my exact need.

    Coming to the issue, I need a macro, wich will copy the mail body (data in tabular format) into excel. Attached macro doing a good job but copying all data into single CELL.

    https://answers.microsoft.com/en-us/office/forum/office_2010-customize/need-to-copy-the-full-data-body-from-outlook-mail/848dfd0d-2d27-4db2-9980-086ccaeabd65?auth=1

    Reply
    • Diane Poremsky says

      July 27, 2016 at 12:21 am

      Do you need to get email address, send date etc? I have this macro: https://www.slipstick.com/developer/code-samples/macro-export-outlook-fields-excel/ If you need the body, you can get that, but probably should run it thru the replace function, to replace double quotes with single quotes.

      If you need to get data out of the body, use regex: https://www.slipstick.com/developer/vba-copy-outlook-email-excel-workbook/

      get multiple data: https://www.slipstick.com/developer/regex-parse-message-text/

      Reply
  29. Dom says

    July 26, 2016 at 10:15 am

    Hi Diane,
    I put a request for your help in last week. Have you had a chance to look at it yet please. It's about digital signatures. Not sure if it's not worked or if it's still under moderation.

    Reply
    • Diane Poremsky says

      July 27, 2016 at 12:23 am

      I was on vacation last week - tried to keep up with some comments, but only ones that were really easy to answer. :)

      Reply
  30. Dom says

    July 13, 2016 at 7:24 am

    Hi,
    I am hoping some one will have a suggestion for me here. I want to digitally sign all outgoing email messages from my account HOWEVER I often send emails FROM other accounts (ie on behalf of) and these don't or can't have digital signatures. If I turn on the setting in Outlook 2013 to "add digital signature to outgoing email messages", I'm forever having to edit the options for individual messages to turn off "sign" for that message, or if I have it permanently off I forget to sign the emails from my personal account.

    What I'm looking for is suggestions as to how I could set up some sort of rule/ script so that all emails from my personal account are signed but those on behalf of others aren't.

    Any thoughts please? Thanks

    Reply
    • Diane Poremsky says

      July 27, 2016 at 12:40 am

      I don't think you can sign mail using a macro (but will check) - you might be able to use a macro to check mail as you send it and if mail from that account is not signed, it will tell you. If not, it can check for mail sent from that account and ask you if you signed it. Samples here: https://www.slipstick.com/how-to-outlook/prevent-sending-messages-to-wrong-email-address/

      Reply
      • Diane Poremsky says

        July 27, 2016 at 8:15 am

        I haven't been able to use Redemption to check outgoing mail for a digital signature (you can use it for incoming mail). Best I have right now is just a warning when you click Send.

      • Dom says

        July 27, 2016 at 7:31 pm

        Thank you for your efforts. I will certainly check out what you've suggested.

  31. steve haley says

    June 14, 2016 at 4:55 am

    Good Morning,
    i am hoping someone one can help me,
    i install CCTV , i have a customer that has a cctv system thats sends email on activation,
    i am unable to set up a schedule on the CCTV as the software designers left this out. so the cctv email is either on or off.
    what i would like to do is forward the emails to my outlook 2016 programme that will forward the emails between 23.00 and 6 in the morning.

    thank you in advanced

    steve

    Reply
    • Diane Poremsky says

      June 14, 2016 at 1:52 pm

      So you want to send the mail to outlook then have outlook forward it if it arrives between during a certain time period? You can either use a run a script rule (makes it easier to filter out the messages) or an item.add macro. To get the time, use code line this in the script or macro:
      If Now() > DateSerial(Year(Now), Month(Now), Day(Now)) + #5:59:00 PM# _
      Or Now() < DateSerial(Year(Now), Month(Now), Day(Now)) + #6:59:00 AM# Then'send the autoreply Set oRespond = Item.Forward With oRespond .Recipients.Add "alias@domain.com" .send end with end if

      Reply
  32. Ashley says

    May 28, 2016 at 8:21 am

    Dear Diane,
    how to set a recurring auto response rule every day between 6PM to 1AM with a custom message?

    Reply
    • Diane Poremsky says

      June 14, 2016 at 1:31 pm

      You can either use a reminder to turn rules on and off or use an itemadd macro to watch for new messages and check the time. I don't currently have any samples that check the hour (but have several that send replies) but putting one together shouldn't be too difficult - check the time using an if statement -
      If Now() > DateSerial(Year(Now), Month(Now), Day(Now)) + #5:59:00 PM# _
      Or Now() < DateSerial(Year(Now), Month(Now), Day(Now)) + #12:59:00 AM# Then'send the autoreply Set oRespond = Item.Reply Set oRespond = Application.CreateItemFromTemplate("C:\path\to\correct.oft") With oRespond .Recipients.Add Item.SenderEmailAddress .Subject = "Office is closed Re:" & Item.Subject .send end with end if

      Reply
  33. Phil says

    April 23, 2016 at 8:59 am

    Post 3 of 3: Steps to run scripts.
    This information is unfortunately lacking in this forum, I had to try several different sources to get my new VBA scripts to work.

    Options: Outlook > File > Options > Trust Centre … Trust Centre Settings > Macro settings > Notification on digitally signed macros
    Start > All Apps > Digital Certificates for VBA (SelfCert) > Create a certificate…
    after cert created, see certificate properties > Install on Local Machine, in Named cert store,
    browse to Trusted Root Certificate Authorities > Import (will see message cert imported ok)
    In VBA window (Alt-F11), Tools > Digital Signature > choose newly created cert, OK
    Close & Return to Outlook; Exit Outlook > Save VbaProject.OTM
    On opening Outlook, after first rule that runs a script starts, must choose Enable Macros.
    Each time exit Outlook, choose to save VbaProject.OTM (even if not changed)

    Reply
    • Diane Poremsky says

      April 23, 2016 at 9:11 am

      Do not sign macros until after they are well tested and working. Each time you edit any macro you need to remove the signature then re-sign it.

      Why are you doing this? "after cert created, see certificate properties > Install on Local Machine, in Named cert store,
      browse to Trusted Root Certificate Authorities > Import (will see message cert imported ok)
      " Selfcert installs the cert - it should immediately be listed in tools, digital signature.

      You should also only need to save VBA once (well twice the first time you exit outlook after signing the code).

      how to self sign video: https://youtu.be/CRbLKo_sPGA

      Reply
    • Susan R says

      April 26, 2016 at 3:37 pm

      Why would I want to get 100s of annoying "notifications" even if the VBA was written by me... stored on my local drive... NOT coming in via email... and I have a self-cert created and installed? Seems like I would need a setting of "just run it, but disable all others".

      Reply
      • Diane Poremsky says

        June 14, 2016 at 1:34 pm

        I'm not sure I understand where you are getting hundreds of notifications from. If you are talking about a self-signed macro, you can trust only that certificate and set security to not trust any others.

  34. Phil says

    April 23, 2016 at 8:54 am

    Post 2 of 3: TailLog

    Public Sub TailLog()
    ' report last 20 emails received, asterisk those received today
    Const logFname = "C:\A\Out_Log.txt"
    Const flagBigFile = 3000, msgBoxLine = 60
    Dim logFile As Integer
    Dim startRec As Integer, thisRec As Integer, maxRec As Integer
    Dim lines() As String, toDate As String, a As String
    toDate = Format(Now(), "yymmdd")
    ' load file into memory
    logFile = FreeFile
    Open logFname For Input As #logFile
    lines = Split(Input$(LOF(logFile), #logFile), vbNewLine)
    Close #logFile
    ' first line is lines(0) despite Option Base 1
    ' report last 20 emails
    maxRec = UBound(lines)
    startRec = maxRec - 20
    If (startRec flagBigFile) Then
    a = a & "WARNING: File " & logFname & " is now over " & _
    flagBigFile & " records." & vbCrLf
    End If
    MsgBox a & "* : received today.", vbOKOnly, "Last 20 emails in " & logFname
    End Sub

    Post concludes with steps required to run scripts in Outlook 2012.

    Reply
  35. Phil says

    April 23, 2016 at 8:46 am

    Hi Diane, I want to contribute a script to your forum, to log which rules have executed. I started in this forum a couple of weeks ago.

    Due to character maximum constraints on posts, I have split this into three posts.
    1 of 3: LogRule

    I have 31 rules. Since each rule script is limited to one argument (outlook.mailitem), I needed to write 31 mini-macros, such as Public Sub Lr29(mail as outlook.mailitem): call LogRule(29, mail): End Sub. Then I have one main script Public Sub LogRule(ruleNumber as integer, mail as mailitem) in which I can do the main work. I have since added a separate script Public Sub TailLog() which I can run using Alt-F8, to display the last 20 items added to the log of rules that have executed, with asterisk showing which rules executed today.
    Option Base 1
    Option Explicit
    Public Sub LogRule(ruleNum As Integer, mail As Outlook.MailItem)
    Const logFname = "C:\A\Out_Log.txt", maxRule = 31, errorRule = 32
    Dim logFile As Integer, ruleName As Variant
    ruleName = Array("Bills Enbridge", "Bills Cogeco", "Bills Bell", _
    "Bills epost", "Kijiji", "Southgate Church", _
    "(ISC)2|ISACA", "HBC", "Michaels", _
    "MSN", "NAC", "Netflix", _
    "Shoppers Drug Mart", "Sears", "Notificn Accepted", _
    "Notificn BNS", "This Week", "Peters by sender", _
    "Peters by subject", "ISSA", "Cn_Books", _
    "epm", "FaithGateway", "Samaritan", _
    "YouVersion", "LinkedIn", "Canadian Tire", _
    "RockAuto", "ebay", "pinterest", _
    "CAA neo", "[not coded]")
    logFile = FreeFile
    Open logFname For Append As #logFile
    If (ruleNum > maxRule) Then ruleNum = errorRule
    Print #logFile, Format(Now(), "yymmdd HH:mm"), _
    "Rule: " & Format(ruleNum, "00") & " " & ruleName(ruleNum), _
    "From: " & mail.SenderName, _
    "Subj: " & mail.Subject
    Close #logFile
    End Sub

    Post continues with code for TailLog...

    Reply
    • Diane Poremsky says

      April 23, 2016 at 9:12 am

      It's really better to post long questions and code in our forum - https://forums.slipstick.com/forums/61-outlook-vba-and-custom-forms/

      Reply
  36. Ram says

    April 20, 2016 at 11:03 pm

    Hi Diane, I am trying to set up a rule wherein I need the script to run first and then start the application. But the priority of actions is the other way around. Is there any way we can change the order of actions selected?

    Reply
    • Diane Poremsky says

      June 14, 2016 at 1:47 pm

      Are you trying to use run a script and start an application actions together? When you use run a script, all actions should be in the script.
      This line in the script should work -
      Shell ("application-name.exe " & "filepath if used"), vbNormalFocus

      office apps would use
      Set oApp = CreateObject("Excel.Application")
      oApp.Workbooks.Open FileName:=strURL
      oApp.Visible = True

      Reply
  37. coolguy says

    March 30, 2016 at 3:29 am

    Hi Diane, i am creating a new outlook rule to trigger a batch file once i get an email having specific text.
    the batch file after running should start my selenium tests but its not doing this.
    i can see batch execution started but the tests are not getting fired.
    can you help me with this?

    any more setting i need to do.?

    Reply
    • Diane Poremsky says

      March 31, 2016 at 3:27 pm

      Does the batch run correctly if you double click on it?

      Reply
      • coolguy says

        April 7, 2016 at 8:09 am

        yes Dianae the scripts are running perfectly well when i double click on it.

        but when i do it through outlook it only flashes the console screen and nothing happens after that...

        thanks in advance.

      • Diane Poremsky says

        June 14, 2016 at 1:42 pm

        As long as you don't need to pass any values to the bat or run it as admin, it should work. It might be better to call the bat from a macro (using a run a script rule) instead of directly from the rule (using the run program rule) - that hands it off from outlook. If you are using run a script, try calling the bat using start an app method.

        This will open a bat file from a macro:
        Shell ("C:\path\to\file.bat"), vbNormalFocus

  38. Bonnie says

    March 25, 2016 at 2:34 pm

    Diane: we recently moved from Outlook 2007 to 2013. I am looking for vba code or a macro that will open an Outlook email message with the header turned off, view the email message in browser (IE) and then save as a PDF document. Our IE Browser has Nuance PowerPDF integrated and would like to use it to create the PDF as it opens a window for naming the file and saving it to a specific location.

    Reply
  39. Alex says

    August 25, 2015 at 4:51 pm

    Is there a way that the script that I am going to invoke first save the attachments that come with the email to some location on file system.Then I want to open the files (these are .csv files)on the disk.Perform certain set of operations between the files and finally I want to send a file that I create after performing the set of operations as a attachment in response to the mail that I had received.Are there any refrences that I can use.

    Reply
  40. Brad says

    July 28, 2015 at 12:59 pm

    Does anyone have a script that:
    On the receive of an email with subject "target this email" it would
    - look in body for text "Email Address:" , take the next 50 chars to use as the email address to send another canned email out.

    So if this is in the body:

    Email Address: testme@email.com

    It would send a canned email to trstme@email.com.

    Thanks in advance.

    Reply
    • Diane Poremsky says

      July 28, 2015 at 11:54 pm

      The rule will take care of the subject part... you need to use regex or instr (or mid) function to find the address. This macro at the end of the article at https://www.slipstick.com/outlook/rules/run-script-rule-change-subject-message/ shows how to do it.

      You'd use myForward.Recipients.Add submatches(1) to add it as the recipient.

      Reply
  41. Michelle Marie says

    July 17, 2015 at 1:44 am

    Hi Diane, I am new to macros and trying to figure out what needs to be added and where in the following code so that only emails being sent to a specific email in Outlook 2010 can run the script. I tried creating a condition for it in Outlook rules but the code didn't work at all when Indid that. I have two emails going to one folder and only need the script run on one email address. I hope that makes sense and you're help is GREATLY appreciated!

    Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
    Dim objAtt As Outlook.Attachment
    Dim saveFolder As String
    Dim dateFormat
    dateFormat = Format(Now, "mm-dd-yy H-mm ")
    saveFolder = "S:\Documents"
    For Each objAtt In itm.Attachments
    If InStr(objAtt.DisplayName, ".pdf") Then
    objAtt.SaveAsFile saveFolder & "\" & dateFormat & objAtt.DisplayName
    End If
    Set objAtt = Nothing
    Next
    End Sub

    Reply
    • Diane Poremsky says

      July 28, 2015 at 11:40 pm

      The condition for "through the specified account" should work. Otherwise, you need an If statement - you can use either address in the if but if using the other address, use Exit Sub

      Dim Recipients As Outlook.Recipients
      Dim recip As Outlook.Recipient
      Dim i

      Set Recipients = Item.Recipients
      For i = Recipients.Count To 1 Step -1
      Set recip = recip & "," & Recipients.Item(i)
      next i
      If InStr(LCase(recip), "your@address") Then
      ' Exit Sub ' if using other address
      ' or
      'code goes here if using the 'right' address
      End if

      Reply
  42. Anthony says

    July 14, 2015 at 6:20 pm

    Hi Diane,
    I have been looking for a way to have Outlook (2013) capture which emails I have active/open before I shut it down (and typically reboot). I have seen some code out there that indicates that in some capacity with the right programming it can be done, such as: https://stackoverflow.com/questions/7471772/when-outlook-loads-to-open-all-emails-that-were-open-when-outlook-last-closed
    But I have not seen it done. It would be very useful for me! Thoughts? thanks!

    Reply
    • Diane Poremsky says

      July 16, 2015 at 2:33 am

      I haven't tried it, but if you capture the entry id of each message, it should work. I don't have any samples. Did you test that code?

      Reply
  43. stolen says

    June 25, 2015 at 4:22 pm

    Hi Diane.

    I need to have meeting invites that arrive in my Inbox for 2 people to show in color and wondered if a script would accomplish this. I get many meeting invites and do not want to overlook the specific invites sent to either of my two bosses (using Outlook 2007), so if they can arrive in the same color for both people or a different color for each, I wanted to ask. Thank you for your help.

    Reply
    • Diane Poremsky says

      June 25, 2015 at 9:16 pm

      Use a Conditional formatting rule in the view.

      Reply
  44. jz says

    June 24, 2015 at 5:51 am

    im using outlook 2007 and wanna allow sending email to only my distribution list contacts and no one else. Need a script for this. Help

    Reply
    • Diane Poremsky says

      June 24, 2015 at 9:36 am

      You'd use an ItemSend macro that checks messages as they are sent - I have a code sample at https://www.slipstick.com/how-to-outlook/prevent-sending-messages-to-wrong-email-address/ - it doesn't check the contacts folder or DL's - just warns if messages aren't sent to specific domains.

      Reply
  45. JB says

    June 17, 2015 at 6:43 pm

    Need some help, I am looking to create a rule in Outlook 2013 that can be applied to any messages that are moved into a specific folder ("ABC123"). I don't see this option anywhere in the Rules Wizard. I want for the action of this rule to be to "Run a Script", but first need to know how to set the rule to be triggered. Select messages will be moved/copied into the ABC123 folder by the user manually. Thanks

    Reply
    • Diane Poremsky says

      June 17, 2015 at 10:36 pm

      A rule only watches the inbox (you can run rules on any folder but only automatic rules work on the inbox). You can use an itemadd macro that watches the ABC123 folder and does something.

      Reply
      • Diane Poremsky says

        June 17, 2015 at 11:51 pm

        I have quite a few itemadd samples on this site - one is the second macro at https://www.slipstick.com/developer/save-attachments-to-the-hard-drive/ - replace the code in the olInboxItems_ItemAdd macro with your code.

        Private Sub olInboxItems_ItemAdd(ByVal Item As Object)
        ' your code here
        End Sub

        oh, and you need to change the application start macro to watch the right folder a subfolder of the inbox would be like this:
        Set olInboxItems = objNS.GetDefaultFolder(olFolderInbox).folders("ABC123").Items

  46. pjop says

    April 1, 2015 at 7:41 am

    Hi guys,

    Do you have an Outlook script on this process?

    I have 3 sub folders under inbox, A_Type Email folder, B_Type Email folder and Forwarded Emails folder

    Whenever I drag emails to either A_Type Email folder or B_Type Email folder, the email will automatically forwarded to a specific emails address and after that it will move to Forwarded Emails folder automatically?

    Thank you for your assistance! I really appreciate it.

    Reply
  47. Agent-311 says

    February 20, 2015 at 8:22 pm

    I know nothing about VBA other than "Alt + F11" and pasting the code into "Module1"

    Current Script that works: Triggered by a Rule & Saves an attachment to a folder

    I Need to catch totally different emails using a different Rule and trigger a 2nd script that does the same as the 1st script, but gives the attachment a different file name.

    It sounds like I can't setup a 2nd Rule to trigger a 2nd script(?). So, how do I do this?

    My Script
    Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
    Dim objAtt As Outlook.Attachment
    Dim saveFolder As String
    saveFolder = "https://website.com/folder/"
    For Each objAtt In itm.Attachments
    objAtt.SaveAsFile saveFolder & "\File Name.txt"
    Set objAtt = Nothing
    Next
    End Sub

    Reply
    • Diane Poremsky says

      March 21, 2015 at 1:22 am

      You can create as many as you need, provided the macro name is different for each.

      Reply
  48. Bryan says

    February 4, 2015 at 12:46 pm

    Hello I am trying to get outlook to send an email to us if the customer does not recieve a report email that gets sent out daily. The code I have currently only causes a pop up box saying that the email was not found. I need it to email us so that we will know that the customer did not recieve the report. Any ideas? Here is the code I am currently using, thank you.
    Private Sub Application_Reminder(ByVal Item As Object)

    If Item.Class = olTask Then
    If InStr(Item.Subject, "subject") > 0 Then
    ReminderUnreceivedMail
    End If
    End If

    End Sub

    Sub ReminderUnreceivedMail()

    Dim Itms As Items
    Dim srchSender As String
    Dim srchSubject As String

    Set Itms = GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items
    srchSender = "sender"
    srchSubject = "xyz"

    Set Itms = Itms.Restrict("[SenderName] = 'sender' And [Subject] = 'subject' And [SentOn] > '" & Format(Date, "yyyy-mm-dd") & "'")

    If Itms.Count = 0 Then
    MsgBox "No " & srchSubject & " email on " & Format(Date, "yyyy-mm-dd")
    End If

    Set Itms = Nothing

    End Sub

    Reply
    • Diane Poremsky says

      February 8, 2015 at 12:02 pm

      In this code block you need to add the code to create a new message or call another macro that creates the message.
      If Itms.Count = 0 Then
      MsgBox "No " & srchSubject & " email on " & Format(Date, "yyyy-mm-dd")
      End If

      Dim objMsg As MailItem

      Set objMsg = Application.CreateItem(olMailItem)

      With objMsg
      .To = "Alias@domain.com"
      .Body = "Whatever"
      .Subject = "This is the subject"
      .Display
      End With
      Set objMsg = Nothing

      Reply
  49. Ken says

    January 31, 2015 at 10:59 am

    Hi Diane, It isOutlook 2010 and there is no Run a script in the rules wizard. Not sure what to do.

    Reply
    • Diane Poremsky says

      February 2, 2015 at 12:25 am

      Run a script should be in all copies of Outlook 2010. What type of email account? Are you using a terminal server?

      Reply
  50. ken says

    January 30, 2015 at 4:02 pm

    Diane, it sounds like the same issue as Meg from August 2014 & the other below it. What are you referring to in your comment " macro script *should* work in thisoutlooksession, but it's best to put it in a new module. And it needs to have the script name line in this format - with Item As Outlook.MailItem:
    Sub MacroName(Item As Outlook.MailItem)" I don't see another reference to macro script

    Reply
    • Diane Poremsky says

      January 30, 2015 at 6:12 pm

      In order for Outlook to use a macro in a run a script rule, the name of the macro needs to be in this format:
      Sub MacroName(Item As Outlook.MailItem)

      Macros with names in this format won't be listed when you click on run a script.
      Sub MacroName()

      I have a ton of macros - but only two can be used in rules:
      Run a script

      Reply
  51. Ken says

    January 30, 2015 at 2:46 pm

    Diane, I am attempting to set up this rule but in the rules wizard under select condition(s) there is not an option to "run a script"

    Reply
    • Diane Poremsky says

      January 30, 2015 at 5:42 pm

      What version of Outlook? It should be near the bottom of the list of actions on the Actions page. (like 4th from the bottom)

      Reply
  52. Jaap says

    January 7, 2015 at 3:38 am

    Thank you very much, Diane! Really appreciated.

    Reply
  53. Jaap says

    December 10, 2014 at 11:53 am

    Hi Diane, did you find the time to look at my code I sent earlier? I appreciate your help very much. Otherwise do you know the code for this part: Set newCalFolder = NS.GetSharedDefaultFolder(objOwner, olFolderCalendar) but then for a mailbox. SInce this example code (part of GetFolderPath macro regards the calender and my issue regards mailboxes.

    Thank in advance,
    Jaap

    Reply
    • Diane Poremsky says

      January 7, 2015 at 1:06 am

      No, I hadn't had a chance yet (I took a lot of time off in Dec.) The getfolderpath macro works with any folder type - it just needs to be told the path. It'll work with all mailboxes open in the profile.

      Reply
  54. Jaap says

    December 5, 2014 at 6:03 am

    Diane, many thanks. I use Exchange 2010 and have -within my own profile jaap@company.com- access to the mailbox of target@company.com. So the emailbox of target@company.com is visible in my left panel in Outlook. The name of that emailbox is 'Target | Company". I know have the following code:

    Private WithEvents Items As Outlook.Items

    Private Sub Application_Startup()
    Set Items = Session.GetDefaultFolder(olFolderSentMail).Items
    End Sub

    Private Sub Items_ItemAdd(ByVal Item As Object)

    If Item.SendUsingAccount = "target@company.com" Then
    ' Get the GetFolderPath function from http://slipstick.me/getfolderpath

    Set Items = GetFolderPath("Target | Company\Sent items").Items

    ' After adding the function to ThisOutlookSession:
    Function GetFolderPath(ByVal FolderPath As String) As Outlook.Folder
    Dim oFolder As Outlook.Folder
    Dim FoldersArray As Variant
    Dim i As Integer

    On Error GoTo GetFolderPath_Error
    If Left(FolderPath, 2) = "\\" Then
    FolderPath = Right(FolderPath, Len(FolderPath) - 2)
    End If
    'Convert folderpath to array
    FoldersArray = Split(FolderPath, "\")
    Set oFolder = Application.Session.Folders.Item(FoldersArray(0))
    If Not oFolder Is Nothing Then
    For i = 1 To UBound(FoldersArray, 1)
    Dim SubFolders As Outlook.Folders
    Set SubFolders = oFolder.Folders
    Set oFolder = SubFolders.Item(FoldersArray(i))
    If oFolder Is Nothing Then
    Set GetFolderPath = Nothing
    End If
    Next
    End If
    'Return the oFolder
    Set GetFolderPath = oFolder
    Exit Function

    GetFolderPath_Error:
    Set GetFolderPath = Nothing
    Exit Function
    End Function

    Set MovePst = GetFolderPath("Target | Company\Sent items")
    Item.UnRead = False
    Item.Move MovePst
    End If

    End Sub

    I tested it, but is does not work yet. What am I doing wrong? Many thanks for your help!

    Reply
    • Diane Poremsky says

      January 7, 2015 at 1:16 am

      This is telling the macro to watch the default sent folder:
      Private Sub Application_Startup()
      Set Items = Session.GetDefaultFolder(olFolderSentMail).Items
      End Sub

      And this tells it to watch the Target Company sent folder:
      Set Items = GetFolderPath("Target | Company\Sent items").Items

      The function goes at the end of the macro, as a separate macro.

      You need this + the function:
      Private WithEvents Items As Outlook.Items

      Private Sub Application_Startup()
      Set Items = Session.GetDefaultFolder(olFolderSentMail).Items
      End Sub

      Private Sub Items_ItemAdd(ByVal Item As Object)

      If Item.SendUsingAccount = "target@company.com" Then
      ' Get the GetFolderPath function from http://slipstick.me/getfolderpath

      Set MovePst = GetFolderPath("Target | Company\Sent items")
      Item.UnRead = False
      Item.Move MovePst
      End If

      end sub

      Reply
  55. Narendra says

    December 5, 2014 at 1:14 am

    Hi Diane,

    I am actually looking for How to auto forward an email with some added explanation. Can you plz help on this

    Reply
    • Diane Poremsky says

      December 5, 2014 at 1:30 am

      like adding a note to it? See https://www.slipstick.com/outlook/rules/run-script-rule-reply-message/

      Reply
  56. Jaap says

    December 3, 2014 at 2:35 am

    Thanks Diane. We have third-party software that uses via Outlook a certain mailbox (not my own, but I have full access to it) to send emails. I want a copy of the sent item put in the Sent folder of that mailbox using a rule. It does so, but no time of sending mail is shown (where the date and time should be shown it says 'none'). The email in my own Sent folder has all the information so now I still need to manually move that email to the correct Sent folder (of that other mailbox). So somehow I think the rule is carried out before the actual sending of the email. Thanks for your reply.

    Reply
    • Diane Poremsky says

      December 5, 2014 at 1:29 am

      It sounds like its moving a draft message. I have a macro here - https://www.slipstick.com/developer/code-samples/use-a-macro-to-move-imap-account-sent-items/ - that moves sent items after they are dropped in the sent folder. Is the mailbox opened in your profile as a shared mailbox or as a separate account? The macro might need tweaked if it's open as a shared mailbox.

      Reply
  57. Jaap Fox says

    December 2, 2014 at 12:49 pm

    Hi Diane, I am using Outlook 2007 and I would like to use a script rule to move a message that has been sent with a certain subject to another folder 30 seconds or 1 minute after it has been sent. Can you help me out?

    Thanks,
    Jaap

    Reply
    • Diane Poremsky says

      December 3, 2014 at 2:01 am

      Outlook doesn't include a timer, so it's difficult to do a delayed move. Is there a reason why it needs to be delayed?

      Reply
  58. Z Tan says

    December 1, 2014 at 4:26 am

    I would like to have a script that when I replied the email, the user defined field " Replied" automatically captured the date of the action. Can anyone advise? I need a step by step guide.

    Reply
    • Diane Poremsky says

      December 5, 2014 at 1:25 am

      You don't need a custom field - you need a custom form (cfg)- get the replied to time one from here - https://www.slipstick.com/exchange/adding-extended-mapi-fields-to-outlook/ - and install it.

      Reply
  59. Hari Prasad says

    November 5, 2014 at 12:31 am

    1. We would like to check and append an Identifier to header (and) Or body of incoming mails in a folder something like . How can we do it in scripts?
    2. This Number should increment automatically.

    Is there a solution?
    Any help is appreciated

    Reply
    • Diane Poremsky says

      November 5, 2014 at 8:30 am

      Adding to the internet header is difficult, but you can add a value to the subject or body field or create a custom field for the number. See https://www.slipstick.com/developer/code-samples/create-custom-numbering-field/ for a sample. It needs converted to a run a script rule.

      Reply
  60. Meg says

    August 22, 2014 at 12:28 pm

    Hi, there is no "run script" option for me to choose from. How do I enable it?

    Reply
    • Diane Poremsky says

      August 22, 2014 at 12:30 pm

      The run a script action is missing from the actions list in the rules wizard? What version of Outlook do you use? What type of email account?

      Reply
      • Erica says

        December 5, 2014 at 10:07 am

        Hello,

        I have the same problem as Meg. The "Run script" option isn't available. I have Outlook 2010, and I'm trying to set the rule for a MAPI account, but I also have an IMAP/SMTP on the same application. If is not possible to use the run script option, is there any other way? Thank you Diane!

      • Diane Poremsky says

        December 6, 2014 at 9:28 am

        The macro script *should* work in thisoutlooksession, but it's best to put it in a new module. And it needs to have the script name line in this format - with Item As Outlook.MailItem:
        Sub MacroName(Item As Outlook.MailItem)

  61. Vaibhav SIngh says

    June 19, 2014 at 5:53 am

    I am trying to open URL upon arrival of E-mail of a particular subject.

    Please help me (Also, I cant see that script when I click on add " SCRIPT")

    Reply
    • Diane Poremsky says

      June 20, 2014 at 12:28 am

      The same url all the time, not one in the message? This page shows how to open a url: https://www.slipstick.com/outlook/tasks/open-webpage-task-reminder-fires/ - put that together with the run a script rule. You could probably forget the script and use the option to run a program, loading the url with a batch file if entering the url as the program name didn't work.

      Create the rule looking for the subject,

      Public Sub OpenURL(Item As Outlook.MailItem)

      Dim strURL As String
      Dim oApp As Object
      Set oApp = CreateObject("InternetExplorer.Application")

      strURL = "http;//www.slipstick.com"

      oApp.navigate (strURL)
      oApp.Visible = True

      'wait for page to load before passing the web URL
      Do While oApp.Busy
      DoEvents
      Loop

      End Sub

      Reply
  62. Egharevba Osarugue says

    April 10, 2014 at 9:41 am

    Hello,
    I am trying to find a way to accurate delete duplicate mails on outlook, or move them to a folder where i could review it before deleting it. Please could you help with a code that could do that or steps to follow to achieve that?

    Reply
    • Diane Poremsky says

      April 10, 2014 at 9:59 am

      I don't have any samples that do that, but basically you need to compare the sender, recipients, sent date, subject and maybe the body. And you need to test each message against every other message in the folder. I'd check the subject first and if it matches, check the sender and sent date. That should be good enough, since you can't send two messages at the exact same time. But to be sure, you could check the recipients and the message body.

      Reply
  63. Dallas M says

    April 3, 2014 at 12:00 pm

    I am trying to change the save destination of the PDF. Where would I replace the folder information?

    P.S. I read and use your codes all the time! I have been able to create a few simple ones of my own because of you. Thanks!

    Reply
    • Diane Poremsky says

      April 3, 2014 at 5:30 pm

      Depending on which macro you are using - it maybe be in this line or one similar:
      strToSaveAs = MyDocs & "\" & sName & ".pdf"
      you can change the path to a fully hardcoded path - like:
      strToSaveAs = "C:\Email\" & sName & ".pdf"

      Reply
  64. Chris D says

    February 20, 2014 at 9:45 am

    Ahh, good to know. Thank you.

    Reply
  65. francesca says

    February 19, 2014 at 5:02 am

    I need to create more projects to manage different rules.
    How can i do?
    Please help me !

    Reply
    • Diane Poremsky says

      February 20, 2014 at 1:54 am

      You can use multiple macros.

      Reply
      • Andrew Jones says

        May 28, 2014 at 10:26 am

        I just stumbled across this when looking for a solution to our needing to save attachments received from 2 different emails to the same folder. I receive these 2 specific emails from the same source at the same time every morning, but need to run two different versions of a script that saves the attachments. One that renames the attachment from one of the emails, and another that leaves the names alone. I know the code needed for each, but can't seem to figure out how to create a new project to get a 2nd script to appear in the list.

      • Diane Poremsky says

        May 28, 2014 at 10:57 am

        Outlook only supports one project. Did you try adding both changes to one script? How are you identifying the messages?

        Are you using Rules to identify the messages, save the attachment, and file the message or using an itemadd macro to save the attachment?

        One option is an if statement -

        if itm.subject = "subject1" then
        'do the save
        else
        'save and rename
        end if

  66. Chris D says

    February 18, 2014 at 1:13 pm

    Say I want to run my "script", which does subject line checking, on every message that comes in, but also allow additional rules to run on those messages that "fall through" my 'IF' statement in my code?

    For example, say I have a mail with the subject "Attachements included" that has attachments I want to save, and another that has the subject "Just Move Me" that I have a rule set for that moves it to a folder. I want the "script" to run on both, but if the subject is "Attachements included", save them (I already have working code for this) and move the message, otherwise, do nothing.

    I put my rule to "run a script" as follows:

    Apply this rule after the message arrives
    on this computer only
    run Project1.ThisOutlookSession.stripAttachmentsFromMailItem

    and another rule after it that says:

    Apply this rule after the message arrives
    with "Just Move Me" in the subject
    and on this computer only
    move it to the "Moved" folder

    My question is if I put "Stop processing more rules" in my "run a script" rule, the additional ones never fire, and I lose my move message functionality, unless I want to code in moves for all of my rules (which I really don't... lol).

    In small testing, it seems to work fine without it, but the warning above specifically states to not process additional rules on messages that have a "script" run on them. I guess I'm just wondering if my set up will work in the long run. Any advice? :)

    Reply
    • Diane Poremsky says

      February 19, 2014 at 12:32 am

      The stop processing will stop looking for rules that match the current message, if it matched the current rule. You don;'t want to use that if you want all rules to check a message.

      As an FYI, the run a script rule should probably be last.

      Reply
  67. Al says

    November 12, 2013 at 2:29 pm

    Apply this rule after the message arrives
    from xxxxxx
    and with xxxxx in the subject
    and on this computer only run xxxxxxxxxxxxx

    Reply
    • Diane Poremsky says

      November 17, 2013 at 10:29 am

      It should work. I'd try removing subject from the rule and adding it to the code - or remove the from line and add it to the code.

      If instr(lcase(item.subject,"keyword") then
      'do whatever
      End if

      Reply
  68. Al says

    November 8, 2013 at 12:35 pm

    My rule doesn't trigger my VBA script when an email enters the Inbox. I know my script works because I can run the rule manually.

    Reply
    • Diane Poremsky says

      November 8, 2013 at 4:17 pm

      How does the rule read?

      Reply
  69. Mark W says

    September 10, 2013 at 2:31 pm

    That seems to have fixed it. Thanks!

    Reply
  70. Mark W says

    September 5, 2013 at 10:18 pm

    I got this to work the first time. It stopped working after I exited Outlook 2013 and went back in. Any ideas?

    Reply
    • Diane Poremsky says

      September 5, 2013 at 10:43 pm

      Check the macro security settings - when this happens it usually means they are not on low.

      Reply
  71. Andy says

    August 30, 2013 at 2:07 am

    Hi,
    I am currently trying to create a script that can be ran as part of rule within Outlook 2013. This will look for a certain subject within the message, then run a script to copy a folder from a server down to my local machine. I believe I have the correct code for this however the script does not appear for selection when creating the rule.

    The code reads -

    Dim fso As FileSystemObject
    Set fso = New FileSystemObject
    fso.MoveFolder "Source", "DeStination"
    Set fso = Nothing

    I have opened VB editor, opened ThisOutlookSession, pasted in the code (with the correct source and destination), then saved the VB project, but this still does not appear for selection. Any help would be greatly appreciated!

    Thanks,

    Reply
    • Diane Poremsky says

      August 31, 2013 at 7:49 pm

      So you are basically using a message to trigger an action on your computer?

      The VBA should look something like this - assuming your code is valid. It can go in either ThisOutlookSession or a module. The key to using a run a script rule is this part of the procedure name: (Item As Outlook.MailItem)

      Public Sub MoveFolder(Item As Outlook.MailItem)
      Dim fso As FileSystemObject
      Set fso = New FileSystemObject
      fso.MoveFolder "Source", "DeStination"
      Set fso = Nothing
      End Sub

      Reply
  72. Richard says

    May 30, 2013 at 6:34 am

    Using VBA to save an attachment with the rule wizard

    Reply
    • Diane Poremsky says

      May 30, 2013 at 9:50 am

      use the macro at Save attachments to drive with a run a script rule that calls the macro:
      Sub SaveAttachmentsRule(Item As Outlook.MailItem)
      SaveAttachments
      End Sub

      Reply
  73. Antonio says

    April 18, 2013 at 4:22 am

    What about runing scripts for OUTGOING messages? Any solution? Thanks

    Reply
    • Diane Poremsky says

      April 18, 2013 at 8:58 am

      Rules scripts are not an option, you need to use the Send event. See Add attachments and set email fields during a mail merge for a code sample.

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Visit Slipstick Forums.
What's New at Slipstick.com

Latest EMO: Vol. 31 Issue 3

Subscribe to Exchange Messaging Outlook






Support Services

Do you need help setting up Outlook, moving your email to a new computer, migrating or configuring Office 365, or just need some one-on-one assistance?

Our Sponsors

CompanionLink
ReliefJet
  • Popular
  • Latest
  • Week Month All
  • Jetpack plugin with Stats module needs to be enabled.
  • Error Opening iCloud Appointments in Classic Outlook
  • Opt out of Microsoft 365 Companion Apps
  • Mail Templates in Outlook for Windows (and Web)
  • Urban legend: Microsoft Deletes Old Outlook.com Messages
  • Buttons in the New Message Notifications
  • Move Deleted Items to Another Folder Automatically
  • Open Outlook Templates using PowerShell
  • Count and List Folders in Classic Outlook
  • Google Workspace and Outlook with POP Mail
  • Import EML Files into New Outlook
Ajax spinner

Recent Bugs List

Microsoft keeps a running list of issues affecting recently released updates at Fixes or workarounds for recent issues in classic Outlook (Windows).

For new Outlook for Windows: Fixes or workarounds for recent issues in new Outlook for Windows .

Outlook for Mac Recent issues: Fixes or workarounds for recent issues in Outlook for Mac

Outlook.com Recent issues: Fixes or workarounds for recent issues on Outlook.com

Office Update History

Update history for supported Office versions is at Update history for Office

Outlook Suggestions and Feedback

Outlook Feedback covers Outlook as an email client, including Outlook Android, iOS, Mac, and Windows clients, as well as the browser extension (PWA) and Outlook on the web.

Outlook (new) Feedback. Use this for feedback and suggestions for Outlook (new).

Use Outlook.com Feedback for suggestions or feedback about Outlook.com accounts.

Other Microsoft 365 applications and services




New Outlook Articles

Error Opening iCloud Appointments in Classic Outlook

Opt out of Microsoft 365 Companion Apps

Mail Templates in Outlook for Windows (and Web)

Urban legend: Microsoft Deletes Old Outlook.com Messages

Buttons in the New Message Notifications

Move Deleted Items to Another Folder Automatically

Open Outlook Templates using PowerShell

Count and List Folders in Classic Outlook

Google Workspace and Outlook with POP Mail

Import EML Files into New Outlook

Newest Code Samples

Open Outlook Templates using PowerShell

Count and List Folders in Classic Outlook

Insert Word Document into Email using VBA

Warn Before Deleting a Contact

Use PowerShell to Delete Attachments

Remove RE:, FWD:, and Other Prefixes from Subject Line

Change the Mailing Address Using PowerShell

Categorize @Mentioned Messages

Send an Email When You Open Outlook

Delete Old Calendar Events using VBA

VBA Basics

How to use the VBA Editor

Work with open item or selected item

Working with All Items in a Folder or Selected Items

VBA and non-default Outlook Folders

Backup and save your Outlook VBA macros

Get text using Left, Right, Mid, Len, InStr

Using Arrays in Outlook macros

Use RegEx to extract message text

Paste clipboard contents

Windows Folder Picker

Custom Forms

Designing Microsoft Outlook Forms

Set a custom form as default

Developer Resources

Developer Resources

Developer Tools

VBOffice.net samples

SlovakTech.com

Outlook MVP David Lee

Repair PST

Convert an OST to PST

Repair damaged PST file

Repair large PST File

Remove password from PST

Merge Two Data Files

Sync & Share Outlook Data

  • Share Calendar & Contacts
  • Synchronize two computers
  • Sync Calendar and Contacts Using Outlook.com
  • Sync Outlook & Android Devices
  • Sync Google Calendar with Outlook
  • Access Folders in Other Users Mailboxes

Diane Poremsky [Outlook MVP]

Make a donation

Mail Tools

Sending and Retrieval Tools

Mass Mail Tools

Compose Tools

Duplicate Remover Tools

Mail Tools for Outlook

Online Services

Calendar Tools

Schedule Management

Calendar Printing Tools

Calendar Reminder Tools

Calendar Dates & Data

Time and Billing Tools

Meeting Productivity Tools

Duplicate Remover Tools

Productivity

Productivity Tools

Automatic Message Processing Tools

Special Function Automatic Processing Tools

Housekeeping and Message Management

Task Tools

Project and Business Management Tools

Choosing the Folder to Save a Sent Message In

Run Rules on messages after reading

Help & Suggestions

Submit Outlook Feature Requests

Slipstick Support Services

Buy Microsoft 365 Office Software and Services

Visit Slipstick Forums.

What's New at Slipstick.com

Home | Outlook User | Exchange Administrator | Office 365 | Outlook.com | Outlook Developer
Outlook for Mac | Common Problems | Utilities & Addins | Tutorials
Outlook & iCloud Issues | Outlook Apps
EMO Archives | About Slipstick | Slipstick Forums
Submit New or Updated Outlook and Exchange Server Utilities

Send comments using our Feedback page
Copyright © 2026 Slipstick Systems. All rights reserved.
Slipstick Systems is not affiliated with Microsoft Corporation.