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.
Related
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.
I have VBA code in Outlook that adds a calendar event in a google calendar when the event is added in Outlook. The code is also able to delete an event in a google calendar when the event is deleted from Outlook.
I have this code at the top of the VBA code:
Dim WithEvents curCal As Items
Dim WithEvents DeletedItems As Items
Dim newCalFolder As Outlook.Folder
The private sub that adds an event looks like this:
Private Sub curCal_ItemAdd(ByVal Item As Object)
Dim cAppt As AppointmentItem
Dim moveCal As AppointmentItem
....
Item.Save
End Sub
The code that deletes an event looks like this:
Private Sub DeletedItems_ItemAdd(ByVal Item As Object)
' only apply to appointments
If Item.MessageClass <> "IPM.Appointment" Then Exit Sub
' if using a category on copied items, this may speed it up.
If Item.Categories = "moved" Then Exit Sub
...
End Sub
Both of these functions work as expected.
Now I need to code a sub that will execute when the event is modified so I can send the modifications to google.
I assume I need to create Private Sub curCal_xxxxxxxxxxxxxxx(ByVal Item As Object) but I dont know what the xxxxxxxxxxxxxxx would be.
Anybody know what the sub name should be where I can place code that will execute after an Outlook calendar event has been changed?
You may consider using the ItemChange event which is fired when an item in the specified collection is changed:
Private Sub myOlItems_ItemChange(ByVal Item As Object)
Dim prompt As String
If VBA.Format(Item.Start, "h") >= "17" And Item.Sensitivity <> olPrivate Then
prompt = "Appointment occurs after hours. Mark it private?"
If MsgBox(prompt, vbYesNo + vbQuestion) = vbYes Then
Item.Sensitivity = olPrivate
Item.Display
End If
End If
End Sub
But there is no information what exactly has been changed, only the item changed is passed as a parameter.
For that reason you may consider handling the AppointmentItem.PropertyChange event which is fired when an explicit built-in property (for example, Subject) of an instance of the parent object is changed. In that case the name of the property that was changed is passed as a parameter.
Also you may find the MailItem.Write event helpful. It is fired when an object is saved, either explicitly (for example, using the Save or SaveAs methods) or implicitly (for example, in response to a prompt when closing the item's inspector).
Public WithEvents myItem As Outlook.MailItem
Private Sub myItem_Write(Cancel As Boolean)
Dim myResult As Integer
myItem = "The item is about to be saved. Do you wish to overwrite the existing item?"
myResult = MsgBox(myItem, vbYesNo, "Save")
If myResult = vbNo Then
Cancel = True
End If
End Sub
Public Sub Initialize_Handler()
Const strCancelEvent = "Application-defined or object-defined error"
On Error GoTo ErrHandler
Set myItem = Application.ActiveInspector.CurrentItem
myItem.Save
Exit Sub
ErrHandler:
MsgBox Err.Description
If Err.Description = strCancelEvent Then
MsgBox "The event was cancelled."
End If
End Sub
Is there a way to ask (popup) if I want to flag the email after I press the send button?
Use the Application.ItemSend event to display a MsgBox asking whether to flag or not.
Then as noted in this question, you'll need to listen to the Items.ItemAdd event on the Sent Items folder and call MarkAsTask on the message passed to the event handler.
So add the following code to ThisOutlookSession - use Alt + F11 to bring up the VB editor.
Private WithEvents Items As Outlook.Items
Private Sub Application_Startup()
Dim SentItems As Folder
Set SentItems = Outlook.Application.GetNamespace("MAPI").GetDefaultFolder(olFolderSentMail)
Set Items = SentItems.Items
End Sub
Private Sub Items_ItemAdd(ByVal Item As Object)
If TypeOf Item Is Outlook.MailItem Then
Dim property As UserProperty
Set property = Item.UserProperties("FlagForFollowUp")
If property Is Nothing Then Exit Sub
Item.MarkAsTask olMarkThisWeek
Item.Save
End If
End Sub
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
If TypeOf Item Is Outlook.MailItem Then
Dim prompt As String
prompt = "Would you like to flag this item?"
If MsgBox(prompt, vbYesNo + vbQuestion, "Flag item") = vbYes Then
Dim property As UserProperty
Set property = Item.UserProperties.Add("FlagForFollowUp", olYesNo)
property.Value = True
End If
End If
End Sub
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'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.