Home > Software design >  Outlook VBA Send Email Event - Passing Parameters
Outlook VBA Send Email Event - Passing Parameters

Time:08-25

I've got two macros which collect data, create and send an email, then continues executing code with the sent email and the data together. The first email prepares some data, adds it to the email and sends. The second macro uses an event handler - adding an item to the sent folder - to continue executing the further code with the data and sent email.

I'm passing data between the macro in my module and the event handler in ThisOutlookSession via a global variable in my module. Even though it works, it seems like bad practice to have global variables accessible anywhere.

What is the 'best practice' way of passing parameters between a macro in a module to an event handler in ThisOutlookSession?

CodePudding user response:

Assuming the event handler only needs to read the data held in the module (ie not write/update it), then the module with the data should hold that data in private module-level variables (aka fields) and make the data available to read either via a Property Get or via a Function ... for example

Private mSomeData As String

Public Property Get SomeData() As String
    SomeData = mSomeData
End Property

Public Function GetSomeData() As String
    GetSomeData = mSomeData
End Function

The data can then be read (but not updated) by your event handler, for example (in this case, the data is held in MyModule)

Debug.Print MyModule.SomeData
Debug.Print MyModule.GetSomeData

Obviously, you only need the Property Get OR the Function ... I include both as examples.

  • Related