I am having no luck dismissing an Outlook alert programmatically before it displays.
Private Sub Application_Reminder(ByVal Item As Object)
Dim objRem As Reminder
Dim objRems As Reminders
If Item.Subject = "TESTING" Then
'downloadAndSendSpreadReport
Set objRems = Application.Reminders
i = 0
For Each objRem In objRems
i = i + 1
If objRem.Caption = "TESTING" Then
objRems.Remove i
If objRem.IsVisible Then
objRem.Dismiss
End If
Exit For
End If
Next objRem
Item.ReminderSet = False
Item.Delete
'Item.Dismiss
End If
End Sub
I want to use this appointment Item as a trigger to some macro without needing users to manually dismiss the reminder.
Seems to me, when this event is triggered, the reminder item is NOT visible, thus cannot be dismissed
I tried to remove it from the reminders set but this seems to delete the whole event from my calendar. Also, it will still display a STRANGE TITLE reminder not in ASCII.
I tried to set the reminderSet property of the appointment item to false, the reminder property still pops up.
So I am looking for a way to a) dismiss the reminder before it pops automatically/ b). dismiss the reminder after it pops automatically....or any workaround to do a scheduled MACRO in Outlook.
(Please note that I do not have permission to use scheduled job in Windows nor VBS.)
Updates:
Now I have the following code. The reminder is being removed, but it will still pop out a reminder window with a caption like "There is no appointment/reminder" something like this.
The beforeReminderShow event is useful in the sense the Reminder Object isVisible = true
so I can dismiss out.. but the reminders windows will continue to pop up even if there's 0 event.
Private WithEvents olRemind As Outlook.Reminders
Private Sub olRemind_BeforeReminderShow(Cancel As Boolean)
Set objRems = Application.Reminders
For Each objRem In objRems
If objRem.Caption = "TESTING" Then
If objRem.IsVisible Then
objRem.Dismiss
End If
Exit For
End If
Next objRem
End Sub
[Solved] - final edit
The final solution workable (I placed in "ThisOutlookSession" Module).
Hope this helps others.
' declare this object withEvents displaying all the events
Private WithEvents olRemind As Outlook.Reminders
Private Sub Application_Reminder(ByVal Item As Object)
Set olRemind = Outlook.Reminders
' RUN OTHER MACRO HERE
End Sub
Private Sub olRemind_BeforeReminderShow(Cancel As Boolean)
For Each objRem In olRemind
If objRem.Caption = "TESTING" Then
If objRem.IsVisible Then
objRem.Dismiss
Cancel = True
End If
Exit For
End If
Next objRem
End Sub
The only way I know how to do this is as follows.
You need this at the top of your module/class:
Private WithEvents olRemind As Outlook.Reminders
Then when you get your Outlook object you need to do this:
Set olRemind = olApp.Reminders
Where olApp is your Outlook Application object.
Now in your code you need to have this event:
Private Sub olRemind_BeforeReminderShow(Cancel As Boolean)
Once you put the WithEvents at the top then you will be able to see all the events you can use.
Now you can cancel this event and thus not see the reminder window.
If you want to dismiss all the reminders, you can simply implement the following code (declare this object WithEvents displaying all the events):
Private WithEvents olRemind As Outlook.Reminders
Private Sub Application_Reminder(ByVal Item As Object)
Set olRemind = Outlook.Reminders
' RUN OTHER MACRO HERE
End Sub
Private Sub olRemind_BeforeReminderShow(Cancel As Boolean)
Cancel = True
End Sub
Related
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
I'm using code like this to catch the open event from an e-mail, but this only fires when you actually double click on an e-mail. Is there a way to also run the code when the e-mail is opened in the preview pane?
Public WithEvents myItem As Outlook.MailItem
Public EventsDisable As Boolean
Private Sub Application_ItemLoad(ByVal Item As Object)
If EventsDisable = True Then Exit Sub
If Item.Class = olMail Then
Set myItem = Item
End If
End Sub
Private Sub myItem_Open(Cancel As Boolean)
EventsDisable = True
'do something
EventsDisable = False
End Sub
Thanks.
That would be the Explorer.SelectionChange event: https://msdn.microsoft.com/en-us/vba/outlook-vba/articles/explorer-selectionchange-event-outlook.
I have looked into previous answers and tried many variations on the below, but I cannot seem to get it to work.
Basically, the purpose of the script below is to turn Outlook reminders into e-mail messages.
However, I cannot seem to programmatically dismiss the reminders. I've looked into prior answers to these questions (such as this one) and attempted to implement them, but they don't seem to succeed in this configuration.
' declare this object withEvents displaying all the events
Private WithEvents olRemind As Outlook.Reminders
Private Sub Application_Reminder(ByVal Item As Object)
Dim objMsg As MailItem
Dim objRem As Reminder
Dim olRemind As Reminders
Set olRemind = Outlook.Reminders
Set objMsg = Application.CreateItem(olMailItem)
objMsg.To = "*ADDRESS REMOVED FROM EXAMPLE*"
objMsg.subject = "MHReminder: " + Item.subject
objMsg.Body = Item.Body
Set objMsg.SaveSentMessageFolder = Session.GetDefaultFolder(olFolderDeletedItems)
objMsg.Send
Set objMsg = Nothing
End Sub
Private Sub olRemind_BeforeReminderShow(Cancel As Boolean)
For Each objRem In olRemind
If objRem.IsVisible = True Then
objRem.Dismiss
Cancel = True
End If
Exit For
Next objRem
End Sub
I'm a bit more of a "kludge-and-splice" programmer than a true programmer, so would appreciate anyone pointing out my (hopefully obvious) errors.
Thank you ...
You are looking at the first found reminder then quitting with
Exit For
As in the example you found Dismiss Outlook reminder you will need something like
For Each objRem In objRems
If objRem.Caption = "TESTING" Then ' <--
If objRem.IsVisible Then
objRem.Dismiss
End If
Exit For
End If
Next objRem
Cancel parameter in the BeforeReminderShow event handler needs to be ByRef.
I want to write a script that changes the format of the mail, when I am replying to a text- or rtf-mail, using Outlook 2013. To have something to begin with. I used the reply event described in the MS dev centre. Unfortunately the example does not work as I expect it to. For testing, I put in a simple message box that should pop up after clicking the reply button. I never see that message box. What did I do wrong?
Public WithEvents myItem As MailItem
Sub Initialize_Handler()
Set myItem = Application.ActiveInspector.CurrentItem
End Sub
Private Sub myItem_Reply(ByVal Response As Object, Cancel As Boolean)
'Set Response.SaveSentMessageFolder = myItem.Parent
MsgBox "I never see this message box :("
End Sub
Do you click Reply in the Explorer or Inspector? Your code will only run if you click Reply button in an Inspector.
To use the method promoted by Microsoft you need this code in ThisOutlookSession. It would be needed if the event code is not in this special class module.
Private Sub Application_Startup()
Initialize_handler
End Sub
The method described in the answer from Max, where code is in Application_Startup rather than Initialize_handler, can be used if all code is in ThisOutookSession.
you have to put this into "ThisOutlookSession" - only there it will work!
Option Explicit
Private WithEvents objInspectors As Outlook.Inspectors
Private Sub Application_Startup()
Set objInspectors = Outlook.Inspectors
end Sub
Private Sub objInspectors_NewInspector(ByVal Inspector As Inspector)
If Inspector.CurrentItem.Class = olMail Then
Set newItem = Inspector.CurrentItem
End If
Set Inspector = Nothing
End Sub
Public Sub newItem_Open(Cancel As Boolean)
newItem.BodyFormat = olFormatHTML
If newItem.Sent = True Then Exit Sub
End Sub
This will work on any new mail-item, I do not know how to make this work only for replys. You could check the subject, if there is an subject already it will be an reply.