Xmedia
New Member
- OS Version(s)
- Windows
- Outlook version
- Outlook 365 32 bit
- Email Account
- Exchange Server
Operating system:: Win 11 Pro
Outlook version: O365
Email type or host: Exchange
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:
Now I always have the problem that the script:
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
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