Run macro automatically when a mail appears in the sent folder

Xmedia

New Member
OS Version(s)
  1. Windows
Outlook version
Outlook 365 32 bit
Email Account
Exchange Server
Operating system::    Win 11 Pro
Outlook version:     O365
Email type or host:    Exchange

Hello everyone,

I am currently struggling with the attempt to automatically execute a macro as soon as a new mail is created in the sent folder.
This one mail should then be marked, the subject should be checked and if the condition is met, the macro should be triggered.

After some searching on the Internet, I have put together a code. But here I still have difficulties:

Here are a few key data:
  • Example for subject: Enquiry | 196630 | OPO_Table$
  • Now I have a script in "ThisOutlookSession", which checks whether a new mail is filed with the sent mails.
  • If so, the subject is checked.
  • If this starts with "Enquiry |", a script should be executed + an info


Now I always have the problem that the script:
  • Does not take the most recent mail - but the second most recent mail.
  • That the script is not triggered at all

Do you have an idea for this - or is there another variant so that a script is executed for every mail sent?
Thank you in advance
Kind Regards
Xmedia


Here is the script:
Private WithEvents SentItems As Items


Private Sub Application_Startup()

Dim OutlookApp As Outlook.Application
Dim ns As Outlook.namespace

Set OutlookApp = Outlook.Application
Set ns = OutlookApp.GetNamespace("MAPI")
Set SentItems = ns.GetDefaultFolder(olFolderSentMail).Items

End Sub


Private Sub SentItems_ItemAdd(ByVal Item As Object)

Dim myItem As Outlook.mailItem
Dim strSubject As String

If TypeOf Item Is mailItem Then
Set myItem = Item
strSubject = myItem.subject

' Check whether the subject begins with "Enquiry |"
If Left(strSubject, 9) = "Anfrage |" Then

'Mark e-mail
myItem.MarkAsTask olMarkToday

' Call up "Macro1"
Macro1

MsgBox "It worked"

End If
End If
End Sub
 
Hi Diane
I'm looking forward to hearing about your experience after testing.
Thank you in advance
 
When testing and if there is an error, you need to click in the app start menu and click run to restart it.
1709004926441.png



Flag in not working - because you arent saving.

'Mark e-mail
myItem.MarkAsTask olMarkToday
myitem.save
 
Hey Diane,
Thanks for the solution. :)
„myitem.save“ was the missing element
It works for me too
 
Hi Diane

I have now realized something.
When Outlook is started, this only works for the 1st mail.
For all other subsequent mails, the macro is no longer called?

Do you have an idea or guess where this is coming from?

Thank you
 
Similar threads
Thread starter Title Forum Replies Date
O Run macro automatically at sending an email Using Outlook 11
Dave A Run macro on existing appointment when it changes Outlook VBA and Custom Forms 1
S VBA Macro - Run-time error '424': object required - Help Please Outlook VBA and Custom Forms 3
B Can't run macro from QAT when emailing from Acrobat Outlook VBA and Custom Forms 0
P Vba script including macro appears in rules but wont run Outlook VBA and Custom Forms 6
Hudas Run Macro when an Email is received Using Outlook 4
L Outlook 2007 Macro to Run a Script From a Contact Form Using Outlook 41
Z Trigger macro to run after a new mail is received in Outlook? Using Outlook 3
D Trigger macro to run when selected email is opened Using Outlook 3
D Create a macro in Outlook to run a rule Outlook VBA and Custom Forms 32
J Can't run macro Outlook 2007 that was created in Word 2007 Outlook VBA and Custom Forms 1
M Run external program from Outlook VBA macro Outlook VBA and Custom Forms 5
J Macro to run on Open Outlook VBA and Custom Forms 2
B Run a macro Outlook VBA and Custom Forms 1
K Macro to Run Rules Outlook VBA and Custom Forms 5
J Outlook, Word, Access crash unless run in Admin mode - time dependent it seems Using Outlook 10
V Outlook macros no longer run until VB editor is opened Outlook VBA and Custom Forms 0
J Want to create a button on the nav bar (module add-in) to run code Outlook VBA and Custom Forms 2
J Outlook Rules VBA Run a Script - Multiple Rules Outlook VBA and Custom Forms 0
N Outlook 2021 'Run Script" Rules? Outlook VBA and Custom Forms 4
T Outlook 2010 Errore run-time -2147417851 (80010105) Metodo delete ContactItem non riuscito Outlook VBA and Custom Forms 0
K Run a script rule to auto 'send again' on undeliverable emails? Outlook VBA and Custom Forms 1
G Save attachment run a script rule Outlook VBA and Custom Forms 0
U Outlook 2019 VBA run-time error 424 Outlook VBA and Custom Forms 2
D Outlook 2013 Macros only run in VB editor, not in drop down or button Outlook VBA and Custom Forms 14
D We're sorry but outlook has run into an error Using Outlook 6
M White square in body of Outlook Messages (O2016 Version 2012 32bit Click To Run) Using Outlook 4
E Having some trouble with a run-a-script rule (moving mail based on file type) Outlook VBA and Custom Forms 5
C Auto Run VBA Code on new email Outlook VBA and Custom Forms 1
Aussie Rules Run a Script on an Incoming Email OK and then the Email reverts Outlook VBA and Custom Forms 0
A Apply Selected Emails to outlook rules and Run Rules Using Outlook 5
B VBScript doesn't run on Recipient Email Outlook VBA and Custom Forms 2
A Run-time error '430' on certain emails when trying to set "Outlook.mailitem" as "ActiveExplorer.Selection.Item" Outlook VBA and Custom Forms 2
S Outlook Custom Form Scripting only working when clicking on "Run this form" Outlook VBA and Custom Forms 2
Y Outlook 2013 Run A Script Outlook VBA and Custom Forms 4
O Outlook 2016 This rule will only run when you check your email in Outlook.... Using Outlook 4
B run scripts Using Outlook 1
A Run time error 424. object required in outlook 2013 Outlook VBA and Custom Forms 10
P errors appear every time I run SCANPST Using Outlook 3
B Outlook rule run a Script doesn't work Outlook VBA and Custom Forms 1
B Wanting to run a script that will filter any body that has a russian link in it. Outlook VBA and Custom Forms 5
P Run Time Error 91 when linking contact to task in VBA Outlook VBA and Custom Forms 1
O Having rules run on old mails noved to inbox Outlook VBA and Custom Forms 8
Bri the Tech Guy Registry Tweak to make "Run a Script" Action Available Outlook VBA and Custom Forms 2
Bri the Tech Guy Run Script rule not running for newly arriving messages Outlook VBA and Custom Forms 25
J Custom form code doesn't run Outlook VBA and Custom Forms 2
J VBA Run When Reply Outlook VBA and Custom Forms 4
Vijay Error in rule- Run a script Using Outlook 1
Vijay Run script doesn't work in outlook Using Outlook 1
O VBA to Run Font Change on Outlook Startup Outlook VBA and Custom Forms 4

Similar threads

Back
Top