Hello,
- I have a perfectly working piece of code in the ThisOutlookSession that traps when the user is sending an email with aApplication_ItemSend in order toadd a characteristic to all sent emails at the beginning of the subject (P- for Product,S- for Service).
From ThisOutlookSession I call the AddCharacteristic function which in turns make use of the UserForm to do it.
I have a couple of questions:
- I was told that The ItemSend event is not a suitable place for displaying any dialog windows (userForm) because they block the UI thread. Is this the case and do you see a problem with the implementation below?
- Do I make right use of Function in Modules?
- Does the Function has to contain error handler as the ThisOutlookSession?
In Forms: TheEmailCharacteristicFormexists
' Characteristic : Product
Private Sub CommandButton1_Click()
Dim Item As MailItem
Set Item = Outlook.Application.ActiveInspector.CurrentItem
Item.Subject = "P- " + Item.Subject
Unload Me
End Sub
' Characteristic : Service
Private Sub CommandButton2_Click()
Dim Item As MailItem
Set Item = Outlook.Application.ActiveInspector.CurrentItem
Item.Subject = "S: " + Item.Subject
Unload Me
End Sub
' If clicks on the close button (X) of the user form, it returns back to the email.
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
Cancel = False
CloseMyForm= True
End If
End Sub
In Modules
' Global variable CloseMyForm takes the value True in case the user closes the user from the (X) button
Public CloseMyForm As Boolean
Function AddCharacteristic() As Boolean
' Initialize global variable closeMyForm to False every time EmailCharacteristicForm is used. Variable becomes True when form closed from (X) button.
CloseMyForm = False
' Show EmailCharacteristicForm .
EmailCharacteristicForm .Show vbModal
' If user closes the EmailCharacteristicForm from by clicking the (X), then should return back to the email.
' AddCharacteristic function returns False when userform is closed by the user and no characteristic is added.
‘ It returns True when the user press a button in the form to add a characteristic
AddCharacteristic = Not CloseMsgBoxFlag
End Function