Trap Outlook calendar event from Access - vba

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?

Related

How do I monitor a newly created outgoing email (pressing 'New Email' or the like) message for changes in MS Outlook?

In brief: I wish to press 'New Email' and then monitor the resulting email for events, specifically the addition of an attachment.
I'm working in VBA under MS Outlook. I am looking to monitor a newly drafted email for any added attachments. My goal is to use the AttachmentAdd event to trigger updates on the email when the attachment is added.
What I think I need to do is to know when the 'New Email' is created that I can start to listen for the event. I do this with other folders by initializing them 'WithEvents'. Thereby, I imagine that I'd wish to do the same with the folder/collection where 'New Email' is spawned. How might I set up such a Listen? Where does a 'New Email' get created in the hierarchy of collections within Outlook? I'm missing where to place my initial hook.
Here is an example of what I do for a folder I know:
Private WithEvents olDeletedItems As Items
'Initialize system to establish locations to monitor
Private Sub Application_Startup()
Dim objNS As NameSpace
Dim objFolder As Outlook.Folder
Set objNS = Application.Session
'Instantiate objects declared WithEvents
Set objFolder = objNS.Folders("me#email.com").Folders("Deleted Items")
Set olDeletedItems = objFolder.Items
End Sub
'Actions on Deleted Items
'Marked deleted items as read as they are deleted
Private Sub olDeletedItems_ItemAdd(ByVal x As Object)
x.UnRead = False
x.Save
End Sub
It seems that the result can be obtained by motoring the 'Inspectors'. Find when a new 'Inspector' is created and then use that to pull the current 'MailItem'. This 'MailItem' can then be monitored for events.
'Monitor for Inspector Events
Private WithEvents olInspectors As Inspectors
'Delcare a place to work with a MailItem that is monitored
Private WithEvents olTempMail As MailItem
'I use this to instantiate all my monitors
Private Sub Application_Startup()
Set olInspectors = Application.Inspectors 'Initialize
End Sub
'Here we look for a new Inspector to be created,
'then pull the item from the inpsector and push it
'to the olTempMail var that we can watch it for events
Private Sub olInspectors_NewInspector(ByVal x As Inspector)
Set olTempMail = x.CurrentItem
End Sub
'Here is a sample event watch where we respond to an
'attachment being added to the MailItem pulled from
'the inspector above.
Private Sub olTempMail_attachmentadd(ByVal x As Attachment)
Debug.Print x.DisplayName
End Sub
In application, this will need to have some filtration and conditionals added to make it work well. But this should get things moving towards being able to react to events that are created for a new MailItem created when the 'New Email' button is pressed in MS Outlook.

Error with Outlook VBA script applied to rule

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.

How to call the quit/close event in outlook 2007 VBA

The question is simple, yet hard to achieve for me for some reason. How can I get to fire the quit/close event when outlook 2007 is being closed?
I want do display a Yes/No msgbox in VBA which executes code depending on the option chosen when outlook is being closed.
I thought I had the solution using:
Dim WithEvents myOlApp As Outlook.Application
Sub Initialize_handler()
Set myOlApp = CreateObject("Outlook.application")
End Sub
Private Sub myOlApp_Quit()
MsgBox "TEST"
End Sub
First I tried to insert it into my Module but this gave me the Only valid in object modules error. So then I created a new class module and pasted the code in here (which gave no errors) but still the event wont fire. What is going wrong and how to fix it?
Set myOlApp = CreateObject("Outlook.application")
There is no need to create a new Outlook instance. You should use the Application property availble in Outlook VBA.
Private Sub Application_Quit()
MsgBox "Goodbye, " & Application.GetNamespace("MAPI").CurrentUser
End Sub
Take a look at the Getting Started with VBA in Outlook 2010 article in MSDN.

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

While itemadd event works in my outlook it doesn't work in friends pc

I have code which works in my system but not in my friend's PC. Both use the same Outlook version.
Here is the snippet.
Private WithEvents olInboxItems As Items
Private Sub start_Click()
Dim objNS As nameSpace
Set objNS = Application.Session
' instantiate objects declared WithEvents
Call accessInbox(inbox) // my own function
Set olInboxItems = inbox.Items
'Set objNS = Nothing
Me.Hide
End Sub
Private Sub olInboxItems_ItemAdd(ByVal Item As Object)
On Error Resume Next
MsgBox "a Message recieved"
'Call download(Item)
Call multiSubjectDownload(Item) //my own function
End Sub
What may be the problem?
Are there settings that differ which prevent the code detecting new mail in inbox?
i found the error i was n't referring to inbox. so event was not firing because my item event was on inbox :)