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
Related
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
I set up a web calendar in office 365 which is connected with "Data Sourse" with Access 2016. Also at the same time the local Outlook is "looking" on the web folder of calendar (outlook at office 365). All three Access (i.e. linked table)-web calendar-local calendar synchronize correctly when adding, changing or removing an appointment.
I would like to "trap" all of the above events and use in my database.
for that reason I added module with ModOutlookRespond with code:
Public Sub Initialize_handler()
Set m_olapp = GetObject(, "Outlook.Application")
m_olapp.GetNamespace("MAPI").Logon "Microsoft Outlook"
Set m_olNameSpace = m_olapp.GetNamespace("MAPI")
'this allows us to capture an appoitment item change event
Set m_olAppoitmentItems = m_olNameSpace.GetDefaultFolder(olFolderCalendar).Items
Debug.Print "Connection Initialised"
End Sub
and added Initialize_handler() on the Open event of Main form.
On the Main form I added also the code:
Public WithEvents m_olapp As Outlook.Application
Public WithEvents m_olAppoitments As Outlook.Items
Public m_olNameSpace As Outlook.NameSpace
Private Sub m_olAppoitments_ItemAdd(ByVal Item As Object)
Debug.Print "Item has added" & Item.Subject
End Sub
I am getting so far only the Debug.Print "Connection Initialised" but not a single time any message from Add event (or any other I tried).
Any ideas what I can try?
I have some outlook VBA code for work that will automatically allocate my team members one email at a time to respond to from our customers, and as it does this it also scans for and gives them any emails that have come in later from the same email address, so the customer can be dealt with in one go.
I want this to run when their own folder becomes empty (i.e. they've dealt with one client, and it automatically runs the above to allocate them another when they move the current mail to an archive, leaving their main inbox folder empty).
Is there any way to do this? I know I can set the macro to check for it every 5 mins, but this will slow Outlook down massively. Any way to trigger the macro only when the user's folder is emptied?
Cheers
Chris
Events are perfect for this.
Events are triggered by the application, when key changes are made. This allows you to avoid using a timed loop.
You can use the WithEvents statement to create a variable that can handle event calls.
In this example the variable f points to the inbox. Whenever an item is deleted from this folder the f_BeforeItemMove procedure is called. It displays the number of items left, minus one. We subtract one because the event is fired before the deletion (this gives you a chance to cancel it, should you wish).
Because we are using an object variable we need to create and destroy it. This occurs when the app is started & exited.
Private WithEvents f As Folder ' Inbox folder, used to monitor events.
Private Sub Application_Startup()
' Register for events.
Set f = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
End Sub
Private Sub Application_Quit()
' Unregister.
Set f = Nothing
End Sub
Private Sub f_BeforeItemMove(ByVal Item As Object, ByVal MoveTo As MAPIFolder, Cancel As Boolean)
' Called when an item is moved out of the inbox.
' Display the number of items left, after delete.
MsgBox (f.Items.Count - 1)
End Sub
This code must be added to the ThisOutlookSession class. The startup and quit events will not fire if pasted into another module.
EDIT
The original solution, above, was trigger just before an item was deleted from the inbox. The OP wanted code that fired just after. This new solution does that.
Private WithEvents f As Folder ' Inbox folder, used to monitor events.
Private WithEvents i As Items ' Items within folder above
Private Sub Application_Startup()
' Register for events.
Set f = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
Set i = f.Items
End Sub
Private Sub Application_Quit()
' Unregister.
Set i = Nothing
Set f = Nothing
End Sub
Private Sub i_ItemRemove()
' Called each time an item is moved out of the inbox.
' This can be triggered by moving an item to another folder
' or deleting it.
' Display the new inbox item count.
MsgBox i.Count
End Sub
As before; this code should be placed inside ThisOutlookSession. You will need to restart Outlook, or manually execute Application_Startup.
You can trap the Items.ItemRemove event to monitor for when the Items.Count property evaluates to 0. Call the Initialize_handler() method during the Application_Startup() event:
Public WithEvents myOlItems As Outlook.Items
Public Sub Initialize_handler()
Set myOlItems = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items
End Sub
Private Sub myOlItems_ItemRemove()
If myOlItems.Count = 0 Then
'Inbox is empty!
End If
End Sub
I have a rule in Outlook which sends a daily email into a particular folder. I then have a VBA script which upon noticing a new unread message in that folder goes in and saves the attachment to a folder on my hard drive and does a few other formatting type things (on the attachment).
I then just linked up the script to the rule in the Outlook rules wizard so it runs as a package.
The problem is as follows: the script is kicked off BEFORE the message is sorted into the appropriate folder. In reality it should run after the message is sorted (otherwise there is nothing for it to act upon). Any ideas on how to rectify?
The code currently begins as follows:
sub saveattachment()
Should it be this instead?
private sub saveattachment()
or
public sub saveattachment()
Would it be better to have the "rule" embedded in the macro instead and then just run it as a private sub anytime the daily email appears in my Inbox?
If you need to assign a VBA macro sub to the Outlook rule, the VBA sub should look like the following one:
Public Sub Test(mail as MailItem)
' your code goes there
End Sub
An instance of the MailItem class is passed as a parameter and stands for the email arrived to the Inbox.
But in case if you need to be sure that your code is triggered when a mail is moved to a particular folder you need to handle the ItemAdd event of the Items class which comes from that folder. Be aware, the event is not fired when more than 16 items are added to the folder simultaneously.
Public WithEvents myOlItems As Outlook.Items
Public Sub Initialize_handler()
Set myOlItems = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderContacts).Items
End Sub
Private Sub myOlItems_ItemAdd(ByVal Item As Object)
Dim myOlMItem As Outlook.MailItem
Dim myOlAtts As Outlook.Attachments
Set myOlMItem = myOlApp.CreateItem(olMailItem)
myOlMItem.Save
Set myOlAtts = myOlMItem.Attachments
' Add new contact to attachments in mail message
myOlAtts.Add Item, olByValue
myOlMItem.To = "Sales Team"
myOlMItem.Subject = "New contact"
myOlMItem.Send
End Sub
Finally, you may find the Getting Started with VBA in Outlook 2010 article helpful.
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.