I got the following situation.
Open an outlook task
The open event is executed
Open a second outlook task
The open event is executed
Close the first task
No close event is executed !!!
Close the second task
The close event is executed
Does anybody have a clue what is happening here ?
Did I do some bad programming ? I have placed my code below.
Thank you in advance.
Kind regards,
Wamor
Public WithEvents objInspectors As Outlook.Inspectors
Public WithEvents objJournal As Outlook.JournalItem
Public WithEvents objTask As Outlook.TaskItem
Dim objOldTask As Outlook.TaskItem
Private Sub Application_Startup()
Set objInspectors = Outlook.Inspectors
Set objTask = Nothing
Set objJournal = Nothing
Set objOldTask = Nothing
End Sub
Private Sub objInspectors_NewInspector(ByVal Inspector As Inspector)
If TypeOf Inspector.CurrentItem Is TaskItem Then
Set objTask = Inspector.CurrentItem
End If
If TypeOf Inspector.CurrentItem Is JournalItem Then
Set objJournal = Inspector.CurrentItem
End If
End Sub
Private Sub objTask_Open(Cancel As Boolean)
MsgBox "TaskItem open event"
End Sub
Private Sub objTask_close(Cancel As Boolean)
MsgBox "TaskItem close event"
End Sub
Of course - you have multiple open objects but only a single variable to store a reference to them.
The standard way of dealing with problems like this is to have a wrapper class that has Inspector and the Item (two items - TaskItem and JournalItem in your case) as variables. TaskOpen and TaskClose event handlers will be methods on that wrapper class.
When NewInspector event fires, create an instance of your wrapper class and populate its properties. Add the wrapper class to a global list.
When close event fires, remove the wrapper class from the list.
Related
I would like to get the dialog box for a follow-up flag and CategoriesDialog box to set a reminder when I close the mailitem.
I tried to modify the code from here. When I close a mailitem, all things remain normal and I get the category dialog. I can not get the dialog box for follow-up flag like this. There is no error message popup.
Public WithEvents objInspector As Outlook.Inspector
Public WithEvents colInspectors As Outlook.Inspectors
Private Sub Application_Startup()
Init_colInspectorsEvent
End Sub
Private Sub Application_ItemLoad(ByVal Item As Object)
Init_colInspectorsEvent
End Sub
Private Sub Init_colInspectorsEvent()
'Initialize the inspectors events handler
Set colInspectors = Outlook.Inspectors
End Sub
Private Sub colInspectors_NewInspector(ByVal NewInspector As Inspector)
If NewInspector.CurrentItem.Class = olMail Then MsgBox "New mail inspector is opened"
If NewInspector.CurrentItem.Class = olTask Then MsgBox "New Task inspector is opened"
If NewInspector.CurrentItem.Class = olContact Then MsgBox "New Contact inspector is opened"
Set objInspector = NewInspector
End Sub
Private Sub objInspector_Close()
If objInspector.CurrentItem.Class = olMail Then 'MsgBox "Mail inspector is closing"
objInspector.CurrentItem.ShowCategoriesDialog
objInspector.CommandBars.ExecuteMso ("AddReminder") 'No error but not work
objInspector.CurrentItem.Save
End If
End Sub
The Outlook object model doesn't provide any property or method for displaying the Add Reminder... dialog.
The best what you can do is to execute a built-in control programmatically:
CommandBars.ExecuteMso ("AddReminder")
But I don't think the Close event handler is the right place for such things.
I have code that auto-zooms the email window pane. It worked until a few days ago after the latest update was made to MS Outlook.
'Install redemption and add "Microsoft Word Object Library" reference and "Redemption Outlook library" reference.
Option Explicit
Dim WithEvents objInspectors As Outlook.Inspectors
Dim WithEvents objOpenInspector As Outlook.Inspector
Dim WithEvents objMailItem As Outlook.MailItem
Dim WithEvents myOlExp As Outlook.Explorer
Dim sExplorer As Object
Dim Document As Object
Dim Msg
Const MsgZoom = 150
Private Sub Application_Startup()
Set objInspectors = Application.Inspectors
Set myOlExp = Application.ActiveExplorer
Set sExplorer = CreateObject("Redemption.SafeExplorer")
End Sub
Private Sub Application_Quit()
Set objOpenInspector = Nothing
Set objInspectors = Nothing
Set objMailItem = Nothing
End Sub
Private Sub objInspectors_NewInspector(ByVal Inspector As Inspector)
If Inspector.CurrentItem.Class = olMail Then
Set objMailItem = Inspector.CurrentItem
Set objOpenInspector = Inspector
End If
End Sub
Private Sub objOpenInspector_Close()
Set objMailItem = Nothing
End Sub
Private Sub objOpenInspector_Activate()
Dim wdDoc As Word.Document
Set wdDoc = objOpenInspector.WordEditor
wdDoc.Windows(1).Panes(1).View.Zoom.Percentage = MsgZoom
End Sub
Private Sub myOlExp_SelectionChange()
On Error GoTo ErrHandler:
Set Msg = Application.ActiveExplorer.Selection(1)
Application.ActiveExplorer.RemoveFromSelection (Msg)
Application.ActiveExplorer.AddToSelection (Msg)
sExplorer.Item = Application.ActiveExplorer
Set Document = sExplorer.ReadingPane.WordEditor
Document.Windows.Item(1).View.Zoom.Percentage = MsgZoom
Exit Sub
ErrHandler:
Exit Sub
End Sub
I have to click on the email, then click it again to get the auto-zoom to work. In the past, I clicked on the email once.
I am using Microsoft Outlook 2016 version 1805 (Build 9330.2087)
The code section that cause the problem is in myOlExp_SelectionChange().
Auto-zooming works in debugging mode when I add a breakpoint in myOlExp_SelectionChange() and step through the code.
Try to use the following call in the event handler before changing the Zoom level:
Application.DoEvents()
The DoEvents function yields execution so that the operating system can process other events. DoEvents passes control to the operating system. Control is returned after the operating system has finished processing the events in its queue and all keys in the SendKeys queue have been sent. DoEvents is most useful for simple things like allowing a user to cancel a process after it has started, for example a search for a file. For long-running processes, yielding the processor is better accomplished by using a Timer or delegating the task to an ActiveX EXE component. In the latter case, the task can continue completely independent of your application, and the operating system takes care of multitasking and time slicing. Any time you temporarily yield the processor within an event procedure, make sure the procedure is not executed again from a different part of your code before the first call returns; this could cause unpredictable results.
Private Sub myOlExp_SelectionChange()
DoEvents
Set Msg = Application.ActiveExplorer.Selection(1)
Application.ActiveExplorer.RemoveFromSelection (Msg)
Application.ActiveExplorer.AddToSelection (Msg)
sExplorer.Item = Application.ActiveExplorer
Set Document = sExplorer.ReadingPane.WordEditor
Document.Windows.Item(1).View.Zoom.Percentage = MsgZoom
End Sub
Also you may try to use a timer for introducing a delay before adjusting the Zoom level. You can use the SetTimer and KillTimer Windows API functions. See Outlook VBA - Run a code every half an hour for more information.
In outlook 2018 onwards, there is an option to save the zoom (please right click on the zoom percentage in the status bar)
I'm trying to send an email based on a calendar reminder going off.
I'm having trouble getting VBA macros to recognize that an Outlook event has occurred.
I put this code in a Class Module:
Public WithEvents myOlApp As Outlook.Application
Sub Initialize_handler()
Set myOlApp = Outlook.Application 'also tried with double quotes around "Outlook.Application"
End Sub
Private Sub myOlApp_Reminder(ByVal Item As Object)
MsgBox ("test")
End Sub
Private Sub myOlApp_NewMail()
MsgBox ("test")
End Sub
When I get a new email or a reminder goes off, nothing happens.
I've tested with this macro in a normal module and it works:
Sub MsgBoxTest()
MsgBox ("test")
End Sub
I have macro settings on "Enable all macros" in the Trust Center.
I've searched google, stackoverflow, a bunch of other websites, and read the documentation on Microsoft.com.
I'm on Outlook 2016 on a PC running Windows 10 Enterprise.
A class module is just a blueprint for an object. A class module doesn't exist all by itself, at runtime a class module is just a type that an object variable can be declared as.
Your code is fine (leaked public field aside).
You're just missing an instance of that class. Keep the class and make ThisOutlookSession create an instance of it:
'[ThisOutlookSession]
Option Explicit
Private AppEvents As AppEventsHandler
Private Sub Application_Startup()
Set AppEvents = New AppEventsHandler
End Sub
Private Sub Application_Quit()
Set AppEvents = Nothing
End Sub
VBA classes fire an Initialize event on creation, and a Terminate event on destruction. Handle them to set your Private WithEvents field:
'[AppEventsHandler] (class module)
Option Explicit
Private WithEvents app As Outlook.Application
Private Sub Class_Initialize()
Set app = Outlook.Application
End Sub
Private Sub Class_Terminate()
Set app = Nothing
End Sub
Private Sub app_NewMail()
'TODO handle app event
End Sub
Private Sub app_Reminder(ByVal Item As Object)
'TODO handle app event
End Sub
'...more handlers...
That's it - now you're handling Outlook.Application events in a dedicated class, without polluting ThisOutlookSession with the nitty-gritty details of every event handler out there.
For this method, often used in documentation, run Initialize_handler manually or run it at startup in the special class module ThisOutlookSession.
Private Sub Application_Startup()
Initialize_handler
End Sub
In order to handle Reminder events, you need to enclose your code in a Sub named "Application_Reminder"
Try this:
Option Explicit
Private Sub Application_Reminder(ByVal Item As Object)
MsgBox "Test"
End Sub
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.
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 :)