I have 6 mailboxes configured in Outlook (x64 365 v 2411), 2x Exchange and 4x MAPI. If is clicked on New Email in the Outlook ribbon, a form is dieplayed where the user can shoose for a Dutch or English message. By default the sender is based on the current selected mailbox in Outlook.
Currently in ThisOutlookSession:
Option Explicit
Private WithEvents objInsp As Outlook.Inspectors
Public objMailItem As Outlook.MailItem
Private Sub objInsp_NewInspector(ByVal Inspector As Inspector)
If TypeName(Inspector.CurrentItem) = "MailItem" Then
Set objMailItem = Inspector.CurrentItem
Load frmNewEmail
With frmNewEmail
.StartUpPosition = 2
.Show False
End With
End If
End Sub
So far no issues, the form is displayed when New Email is clicked. However, after btnEnglish is clicked:
Option Explicit
Private Sub btnEnglish_Click()
Me.hide
Call MyNewMessage(2)
Unload Me
End Sub
The expected behavior is that objMailItem still holds the value when the next procedure is started in Modules > MyFunctions:
Option Explicit
Public objMailItem As Outlook.MailItem
Public Sub MyNewMessage(lngLangID As Long)
Dim objMsg As Object
Dim strSigID As String
AddTable objMailItem
strSigID = GetSignatureID(objMailItem, lngLangID)
VerifySignature objMailItem, strSigID
With objMailItem
.Subject = "[Case No. 000000] "
End With
frmNewEmail.hide
Unload frmNewEmail
Set objMailItem = Nothing
End Sub
Here I notice that objMailItem does not have a value any longer. I tried to use objMailItem as form tag and pass it from there, but also there objMailItem has no value. Trying for hours with alternative options did not help.
How can I maintain the value of objMailItem and pass it on to procedures in other forms and modules?
Art.
You can avoid the global variable and pass the mail item directly to your userform:
Option Explicit
Private WithEvents objInsp As Outlook.Inspectors
Private Sub objInsp_NewInspector(ByVal Inspector As Inspector)
Dim frm As frmNewEmail, msg As Outlook.MailItem
If TypeName(Inspector.CurrentItem) = "MailItem" Then
Set frm = New frmNewEmail 'avoid using the default instance...
With frm
Set .Message = Inspector.CurrentItem 'pass message directly to userform
.StartUpPosition = 2
.Show False
End With
End If
End Sub
Add a Message
Property to your userform:
Option Explicit
Private m_msg As Outlook.MailItem
Private Sub btnEnglish_Click()
Me.hide
MyNewMessage m_msg, 2 'use of Call is deprecated
Unload Me
End Sub
'called from the code launching the form
Property Set Message(v As Outlook.MailItem)
Set m_msg = v
End Property
Edit MyNewMessage
to add the message reference as a parameter:
Public Sub MyNewMessage(msg As Outlook.MailItem, lngLangID As Long)
'work with msg and lngLangID
End Sub