Can someone help me understand ThisOutlookSession and Class modules?
The code I've written, works fine if it's all in ThisOutlookSession, but the code is getting quite large now so I want to try and break it out in to multiple Class modules.
I basically have both ItemSend and ItemAdd events, and none of these fire if I put them in Class module.
I think the problem is Initialize_handler are never being fired, but I'm not sure how best to accomplish that.
On ItemSend, as above I am detecting unchanged fields from an OFT file. And on ItemAdd, I am saving the sent email to a Windows folder.
Class1 module (ItemSend):
Option Explicit
Public WithEvents App As Outlook.Application
Public Sub Initialize_handler()
Set App = CreateObject("Outlook.Application")
End Sub
Private Sub App_ItemSend(ByVal Item As Object, Cancel As Boolean)
' do something here
End Sub
Class2 module (ItemAdd):
Option Explicit
Dim App As New Outlook.Application
Private WithEvents SentItems As Outlook.Items
Public Sub Initialize_handler()
Set SentItems = App.GetNamespace("MAPI").GetDefaultFolder(olFolderSentMail).Items
End Sub
Private Sub SentItems_ItemAdd(ByVal Item As Object)
'do something here too
End Sub
Module2 module (GenerateEmail) - stripped out to keep it short:
Option Explicit
Public Entity As String
Public IRNs() As String
Public Sub NewClientEmail()
Entity = "I"
GenerateEmail (Entity)
End Sub
Public Sub NewAgentEmail()
Entity = "A"
GenerateEmail (Entity)
End Sub
Private Sub GenerateEmail(Entity As String)
Dim App As Outlook.Application
Dim Item As Outlook.MailItem
Dim Connection, Command, Recordset As Object
Dim InputIRNs, Rows As Variant
Dim SqlCon, InputIRN, Client, Country As String
Dim IRNsWithStaff(), ClientRefs(), AppNos() As String
Dim d As Dictionary
Dim i, r, dCount As Integer
Set App = New Class1
App.Initialize_handler
Set Item = App.CreateItemFromTemplate(Template(Entity))
With Item
'do something
End With
End Sub
The App.CreateItemFromTemplate line (highlighted) fails, because CreateItemFromTemplate does not exist in App, even though App is from Outlook.Application from Class1 module.