I want to answer Wordpress contact form request with Outlook and want to automatically attach the AGB of my company as a PDF file everytime I respond. So I need to catch the sender and respond to the deposited Mail in the form. Is there a possibility to do this with VBA?
Thanks and regards from Germany
I just tried this code but nothing happend (I am a fully VBA noob):
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
If obj.SenderEmailAddress = "wendt#getweb.io" Then
Sub NewMessageWithAttachment()
Dim oMsg As Outlook.MailItem
Set oMsg = Application.CreateItem(olMailItem)
With oMsg
.Attachments.Add "C:\\Users\\John Wendt\\Downloads\\schaum.jpg"
.Display
End With
End Sub
First of all, you need to remove the sub declaration from the ItemSend event handler in your code:
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
If obj.SenderEmailAddress = "wendt#getweb.io" Then
' do your stuff for the sender
End If
End Sub
Sub NewMessageWithAttachment()
Dim oMsg As Outlook.MailItem
Set oMsg = Application.CreateItem(olMailItem)
With oMsg
.Attachments.Add "C:\\Users\\John Wendt\\Downloads\\schaum.jpg"
.Display
End With
End Sub
Be aware, the ItemSend event handler is used for outgoing emails.
If you need to intercept incoming emails you may consider using the NewMailEx event instead. This event fires once for every received item that is processed by Microsoft Outlook. The item can be one of several different item types, for example, MailItem, MeetingItem, or SharingItem. The EntryIDsCollection string contains the Entry ID that corresponds to that item. Use the Entry ID represented by the EntryIDCollection string to call the NameSpace.GetItemFromID method and process the item. The NewMailEx event fires when a new message arrives in the Inbox and before client rule processing occurs.
See Getting started with VBA in Office to be more familiar with VBA in general.
Related
I'm trying to record a specific next-numbering on certain e-mails after I send them. However Application_ItemSend executes the code before it sends the e-mail.
I'm adding a reference number to the Subject, but I don't want to confuse everyone with random numbers appearing in their inbox.
Is there a way to execute code after the send?
I think I got it:
Declare a new type of events-related variable to ensure every time an e-mail is added to the "Received Items" folder an action is executed.
Private WithEvents Rcvditems As Outlook.Items
The method I used to avoid variables being restored to 'nothing' is using the same Application.Item_Send event. This option will ensure I activate my reader every time I send an e-mail.
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set Rcvditems = objNS.GetDefaultFolder(olFolderSentMail).Items
End Sub
... what it is doing is recalculating my variable 'Rcvditems'.
Finally, as Rcvd_Items is an event-related variable, I can assign event 'AddItem' to it.
Private Sub Rcvditems_ItemAdd(ByVal Item As Object)
On Error GoTo EscapeSub
Call MyMacro(Item)
EscapeSub:
End Sub
Like this I'm ensuring that I won't alter the e-mail when prior sending it, but just the e-mail that is added in the Received-Items folder.
By default, the sent items are placed to the Sent Items folder in Outlook. If so, you can hook to the ItemAdd event of the Items class which belongs to the Sent Items folder. It is fired when one or more items are added to the specified collection. This event does not run when a large number of items are added to the folder at once (more than sixteen) - this is a known issue in OOM and exists in all Outlook versions.
Public WithEvents myOlItems As Outlook.Items
Public Sub Initialize_handler()
Set myOlItems = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderSentItems).Items
End Sub
Private Sub myOlItems_ItemAdd(ByVal Item As Object)
MsgBox Item.Subject
End Sub
Note, the MailItem.SaveSentMessageFolder property allows to set a Folder object that represents the folder in which a copy of the email message will be saved after being sent. So, it is not necessary that an Outlook item will be saved to the Sent Items folder if an add-in sets this property in the ItemSend event handler.
Why am I facing the below problem?
I have written a code to open sent email after sending email. The VBA code doesn't open the latest sent email but the previous one.
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim myItem As MailItem
Dim myNamespace As NameSpace
Dim myFolder As Folder
Set myNamespace = Application.GetNamespace("MAPI")
Set myFolder = myNamespace.GetDefaultFolder(olFolderSentMail)
Set myItem = myFolder.Items(myFolder.Items.Count)
myItem.Display
End Sub
This is the answer why this won't work with the Application_ItemSend event:
Application_ItemSend is called before the email is sent. You can see that because it has a Cancel parameter. This means the email only gets sent if Cancel = False else the email is dropped.
So you just cannot display the email because it isn't sent at this time in the Application_ItemSend event. It gets sent after the Application_ItemSend is finished.
Note: If you put a break point within the Application_ItemSend you will see that the "New Email" window remains open/visible until the ItemSend event is finished. Therefore you cannot open that email within that event.
Workaround
You might try the following code. This creates an event for the default folder of sent items which is called when an item is added to this folder.
A restart of Outlook might be needed after adding the code (or at least running the Application_Startup procedure once).
Option Explicit
Public WithEvents myOlItems As Outlook.Items
Private Sub Application_Startup()
Set myOlItems = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderSentMail).Items
End Sub
Private Sub myOlItems_ItemAdd(ByVal Item As Object)
Item.Display
End Sub
If the folder where your sent items are moved to is not the default Outlook sent mail folder then you need to find the right folder to set it in Application_Startup.
I have a macro that prompts you to save an email to a specified folder before you send it, but I can't get the macro to work for receiving emails.
This is what I have for sending emails:
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
If TypeOf Item Is Outlook.MailItem Then
Cancel = Not SaveSentMail(Item)
End If
End Sub
Private Function SaveSentMail(Item As Outlook.MailItem) As Boolean
Dim F As Outlook.MAPIFolder
If Item.DeleteAfterSubmit = False Then
Set F = Application.Session.PickFolder
If Not F Is Nothing Then
Set Item.SaveSentMessageFolder = F
SaveSentMail = True
End If
End If
End Function
Work with Application.Startup Event (Outlook) and Items.ItemAdd Event (Outlook)
Items.ItemAdd Event (Outlook) Occurs when one or more items are added to the specified collection. This event does not run when a large number of items are added to the folder at once. This event is not available in Microsoft Visual Basic Scripting Edition (VBScript).
Application.Startup Event (Outlook) Occurs when Microsoft Outlook is starting, but after all add-in programs have been loaded.
Code Example
Private WithEvents Items As Outlook.Items
Private Sub Application_Startup()
Dim olNs As Outlook.NameSpace
Dim Inbox As Outlook.MAPIFolder
Set olNs = Application.GetNamespace("MAPI")
Set Inbox = olNs.GetDefaultFolder(olFolderInbox)
Set Items = Inbox.Items
End Sub
Private Sub Items_ItemAdd(ByVal Item As Object)
Dim Folder As Outlook.MAPIFolder
If TypeOf Item Is Outlook.mailitem Then
Set Folder = Application.Session.PickFolder
If Folder Is Nothing Then Exit Sub
Item.Move Folder
End If
End Sub
You can handle the NewMailEx event of the Application class which is fired when a new message arrives in the Inbox and before client rule processing occurs. You can use the Entry ID returned in the EntryIDCollection array to call the NameSpace.GetItemFromID method and process the item. Use this method with caution to minimize the impact on Outlook performance.
Be aware, for users with an Exchange Server account (non-Cached Exchange Mode or Cached Exchange Mode), the event will fire only for messages that arrive at the server after Outlook has started. The event will not fire for messages that are synchronized in Cached Exchange Mode immediately after Outlook starts, nor for messages that are already on the server when Outlook starts in non-Cached Exchange Mode.
Another possible option is to handle the ItemAdd event of the Items class which is fired when one or more items are added to the specified collection. Be aware, this event does not run when a large number of items are added to the folder at once (more than 16 - this is a well-known issue in OOM).
A better solution is to check for new items in the folder using the timer's event. Read more about possible solutions in the following articles:
Outlook NewMail event unleashed: the challenge (NewMail, NewMailEx, ItemAdd)
Outlook NewMail event: solution options
Outlook NewMail event and Extended MAPI: C# example
Outlook NewMail unleashed: writing a working solution (C# example)
I've written a macro to add BCC address on reply window. But I want to do the same on click of 'Reply' Button. I can not add macro to this button as it is not custom button. How should I do this?
You can repurpose built-in controls. But in that case you need to develop an add-in, not a VBA macro. See Temporarily Repurpose Commands on the Office Fluent Ribbon for more information.
Also you may try to handle the ItemSend event of the Application class which is fired whenever an Microsoft Outlook item is sent, either by the user through an Inspector (before the inspector is closed, but after the user clicks the Send button) or when the Send method for an Outlook item, such as MailItem, is used in a program. In the event handler you may try to add a new entry to the Recipients collection (see the corresponding property) with the 'Type' property set to olBcc.
This is from superuser.
https://superuser.com/questions/327614/outlook-macro-to-interrupt-a-reply-all
"You can add an event handler via VBA to pick up the ReplyAll event. Something like the following:"
Dim WithEvents insp As Outlook.Inspectors
Dim WithEvents mailItem As Outlook.MailItem
' This is called on Outlook startup
Private Sub Application_Startup()
Set insp = Application.Inspectors
End Sub
' This is called when a new Inspector is created.
' You use it to pick up on a new mail item event
Private Sub insp_NewInspector(ByVal Inspector As Inspector)
' Edit: The size test appears to be incorrect
'If Inspector.CurrentItem.Size = 0 And Inspector.CurrentItem.Class = olMail Then
If Inspector.CurrentItem.Class = olMail Then
Set mailItem = Inspector.CurrentItem
End If
End Sub
' Called when you press ReplyAll
Private Sub mailItem_ReplyAll(ByVal Response As Object, Cancel As Boolean)
Dim msg As String
Dim result As Integer
msg = "Do you really want to reply to all?"
result = MsgBox(msg, vbYesNo, "Reply All Check")
If result = vbNo Then
Cancel = True
End If
End Sub
Put the code in the ThisOutlookSession module then restart.
I have the code below in which the SenderName MailItem property appears blank.
Private Sub Application_ItemSend(ByVal MyItem As Object, Cancel As Boolean)
Dim res As Integer
' The "Class" MailItem property is found
If MyItem.Class = olMail Then
' This is coming up blank
' The "SenderName" MailItem property is not apparent
MsgBox MyItem.SenderName
res = MsgBox("Archive outgoing email?", vbQuestion + vbYesNo, "XYZ")
If res = vbYes Then
Call ProcessIt(MyItem)
Else
Cancel = True
End If
End If
End Sub
Some schoolboy error perhaps and will appreciate pointers as to what I am missing.
Outlook 2013
Windows 7
Sender related properties are populated only after the message is actually sent and moved to the Sent Items folder. By the time Application.ItemSend event fires, the message is still unsent.
The earliest you can access the sent message with all the properties populated is in the Items.ItemAdd event on the Sent Itens folder.