Auto run when an appointment is updated - vba

I would like to automatically run a macro when an appointment is updated.
For my test, I wish this macro displays the subject of the appointment.

You want the ItemChange event http://msdn.microsoft.com/en-us/library/office/ff865866%28v=office.14%29.aspx
Public WithEvents myOlItems As Outlook.Items
Public Sub Application_Startup()
Set myOlItems = _
Application.GetNamespace("MAPI").GetDefaultFolder(olFolderCalendar).Items
End Sub
Private Sub myOlItems_ItemChange(ByVal Item As Object)
debug.print item.subject
End Sub

This can be done by creating a rule in Outlook, and will start a macro. If you have your macro working right, then you need only set the rule. First:
Sub Sub_name(MyMail As MailItem)
'Working Code
End Sub
Then, set the condition to "which is a meeting invitation or update", actions are "run a script" Check on it, then click on the "script" link and select Sub_name.

Related

Trigger VBA code when changing to a different mail in reading pane

Once I read new mails from a specific sender I would like to move them from the Inbox to a subfolder.
I set Outlook to mark mails as read as soon as I open them. To make sure I can read through the mail I want to move the mail only when I close it/change to a different mail. (Note that I'm using the reading pane.)
I tried the Explorer.SelectionChange event but it triggers multiple times when I change to a new mail.
Private WithEvents expl As Outlook.Explorer
Private Sub Application_Startup()
Set expl = Application.ActiveExplorer()
End Sub
Private Sub expl_SelectionChange()
MsgBox "Selection changed"
End Sub
Why does this trigger multiple times?
How do I get a reference to the mail item I'm "closing"?
Why does this trigger multiple times?
I guess (I could be wrong), it is because of the View that you have. The above said behaviour was not noticed for RPO or Preview mode. When you have, say, a Compact view the selection change happens two times? First when the item is selected and the 2nd time when the item contents are displayed. But like I said, I could be wrong...
Alternative
It would have been easier if Outlook had Application.EnableEvents = False like MS Excel. Hope this alternative helps?
Option Explicit
Private WithEvents expl As Outlook.Explorer
Dim DisableEvents As Boolean
Private Sub Application_Startup()
Set expl = Application.ActiveExplorer()
End Sub
Private Sub expl_SelectionChange()
If DisableEvents = True Then
DisableEvents = False
Exit Sub
End If
MsgBox "Selection changed"
DisableEvents = True
End Sub

How to close or dismiss an Outlook Reminder via Outlook VBA

I want to run a Macro in Outlook at a certain time and so I'm using the Outlook Reminders to do it. I have written the below code, which successfully runs the Macro but after it has finished the If statement, it then pops up the reminder which I don't need to see and so therefore need to close/dismiss it.
Public Sub Application_Reminder(ByVal Item As Object)
If Item.Subject = "Refresh Data Test" Then
Call RunExcelMacros.TestRun
End If
End Sub
Please can someone help suggest how I can dismiss the reminder?
Okay, I think I've got it - the below seems to work, all code is setup in the "ThisOutlookSession" Module:
Private WithEvents OutlookReminders As Outlook.Reminders
Public Sub Application_Reminder(ByVal Item As Object)
Set OutlookReminders = Outlook.Reminders
If Item.Subject = "Refresh Data Test" Then
Call RunExcelMacros.TestRun
End If
End Sub
Private Sub OutlookReminders_BeforeReminderShow(Cancel As Boolean)
Dim OutlookReminder As Reminder
'After the "Application_Reminder" has run it will then run this code straight after which stops the reminder from actually popping up
For Each OutlookReminder In OutlookReminders
If OutlookReminder.Caption = "Refresh Data Test" Then
If OutlookReminder.IsVisible Then
OutlookReminder.Dismiss
Cancel = True
End If
Exit For
End If
Next OutlookReminder
End Sub

Autofill BCC address

I want to autofill the BCC field with a specific address on replies, forwards and new emails.
I have seen a similar function that performs "silently" - i.e., the BCC address is added once the 'Send' button has been pressed.
I want to be able to remove/change the address if necessary.
From the user's perspective: click reply/forward/new email, and the message window opens up with the BCC field filled.
My knowledge of VBA is somewhat limited, so I'd appreciate if you could be specific about where to place the code.
This code put in ThisOutlookSession module will fire when you click on the button New Email and when replying to an email. From there it's simple to insert whatever you need in the various fields. You need to restart Outlook or manually call Application_Startup() to have it activated the first time.
Option Explicit
Public WithEvents myInspectors As Outlook.Inspectors
Public WithEvents myExplorer As Outlook.Explorer
Private Sub Application_Startup()
Set myInspectors = Application.Inspectors
Set myExplorer = Application.ActiveExplorer
End Sub
Private Sub myInspectors_NewInspector(ByVal Inspector As Inspector)
If TypeName(Inspector.CurrentItem) = "MailItem" Then
'MsgBox "new mail"
Inspector.CurrentItem.BCC = "joe.doe#domain.com"
End If
End Sub
Private Sub myExplorer_InlineResponse(ByVal Item As Object)
'MsgBox "reply"
Item.BCC = "jane.dane#domain.com"
End Sub

Is it possible to run an Outlook macro when a new Contact is created/saved?

I have an Outlook macro that edits certain fields in all of my Contacts. Is it possible to auto-trigger this macro to run whenever a new Contact is created/saved?
Add this code to the ThisOutlookSession module:
Private WithEvents objNewContact As Items
Private Sub Application_Startup()
Set objNewContact = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderContacts).Items
End Sub
Private Sub objNewContact_ItemAdd(ByVal Item As Object)
MsgBox Item.CompanyAndFullName & " added"
End Sub
Private Sub objNewContact_ItemChange(ByVal Item As Object)
MsgBox Item.CompanyAndFullName & " changed"
End Sub
Application_StartUp will set objNewContact to watch the contacts folder when you start Outlook.
ItemAdd will fire when you save the contact.
ItemChange will fire when you save an existing contact after changing it.
As the link provided by #Maciej states - The sample code must be placed in a class module and ThisOutlookSession is a class module.

Execute VBA to Change Reminder Time Based on Calendar

ALL events created in outlook 2007 have the same default reminder time. It seems you cannot change this time for individual calendars.
I have two calendars I use within outlook: my normal "events" calendar and a separate calendar I use to track task items I want to work on at specific times. I want them to have different reminder times (specifically for the tasks one, 0 minutes).
I would like to make a macro to execute every time an event is created to:
check calendar name
change reminder time for events belonging to 1 calendar (to 0 min)
Any resources would be appreciated. I've done significant VBA programming in Excel but documentation on Outlook vba (as well as a lack of "record macro" in outlook) makes it hard because it is really not intuitive for me.
I am trying something (kinda shooting in the dark based on lots of searching..) like this (as a class module)
Dim myOlApp As New Outlook.Application
Public WithEvents myOlItems As Outlook.Items
Public Sub Initialize_handler()
Set myOlItems = myOlApp.GetNamespace("MAPI").GetDefaultFolder(olFolderCalendar).Items
End Sub
Private Sub myOlItems_ItemAdd(ByVal Item As Object)
Dim myCAlEntry As Outlook.MeetingItem
MsgBox ("test")
End Sub
I should clarify: I am unable to see "test" in a message box when creating calendar events right now.
In ThisOutlookSesion:
Public Sub Application_Startup()
Set myOlItems = myOlApp.GetNamespace("MAPI").GetDefaultFolder(olFolderCalendar).Items
End Sub
You do not need Dim myOlApp As New Outlook.Application:
Public Sub Application_Startup()
Set myOlItems = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderCalendar).Items
End Sub
The original setup should work with:
Public Sub Application_Startup()
Initialize_handler
End Sub