Passing object variables to controls within a userform

  • Thread starter Thread starter Mike
  • Start date Start date
M

Mike

Hi - I'm new to VBA for Outlook - please excuse if you come across any
obvious blunders.

I'm using a userform to handle items in various ways according to the
choices provided with controls in the userform. I've got the mailitem
via set statement as below (where MyNS As NameSpace, myInbox As
MAPIFolder, theMsg As MailItem, MoveTo As MAPIFolder are globally
declared):

Private Sub UserForm_Initialize()
Set MyNS = GetNamespace("MAPI")
Set myInbox = MyNS.GetDefaultFolder(olFolderInbox)
Set theMsg = myInbox.Items.GetFirst
... onwards
end sub

What I want to do now is pass the mailitem as set in theMsg on to the
click event of a command button in the form.

How do I do that?
 
Create a module level object for your mail item and set that either instead
of the local mail item or at the end of the procedure set the module level
object from the local one. A module level object would be declared before
any other code in your UserForm.
 
Use a global variable:

Dim theMsg As Object

Private Sub UserForm_Initialize()
Set MyNS = GetNamespace("MAPI")
Set myInbox = MyNS.GetDefaultFolder(olFolderInbox)
Set theMsg = myInbox.Items.GetFirst
... onwards
end sub

Private Sub CommandButton1_Click()
' do stuff with theMsg
End Sub
 
Heya! - Thanks for tipping me off (didn't think of doing so at module
level).

Seeing that I'm only interested in one mail item (Items.GetFirst),
it's tempting to skip MyNS As NameSpace, myInbox As MAPIFolder and
TheMsg As MailItem and just go directly at module level using

Set TheMsg = GetNamespace
("MAPI").GetDefaultFolderolFolderInbox).Items.GetFirst

All code now boils down to the below which appears to work
satisfactory. Would you rate this as decent code?

This outlooksession
Private Sub Application_Startup()
Load ufMailalert
End Sub

Private Sub Application_NewMail()
NewMailAlert
End Sub


Module level (Module1)
Global TheMsg As Object

Sub NewMailAlert()
Set TheMsg = GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items.GetFirst
ufMailalert.Show modal
End Sub

Userforms (ufMailalert)
Private Sub UserForm_Initialize()

Dim Sender As String, Subject As String
Sender = TheMsg.SenderName
Subject = TheMsg.Subject

ufMailalert.txFrom = Sender
ufMailalert.txSubj = Subject

cmdWait.SetFocus
End Sub

Private Sub cmdDelete_Click() ‘
TheMsg.UnRead = False
TheMsg.Delete

Set TheMsg = Nothing

ufMailalert.Hide
End Sub

Private Sub cmdOther_Click() ‘
Lots of other stuff
End Sub
 
It is better in the Outlook world to get and dereference each object
separately. Otherwise, in some cases, you may get memory leaks or be unable
to shut down Outlook completely.

Also, you might need to do some thinking and experimenting about what
GetFirst returns. It may not be what you expect.
 
You're absolute right! - I have wondered about the difference of
initialize and activate event - solved the problem using activate
(after consulting the VBA help on Load statement). If you were around
I would probably kiss you!
As the process fires on Application_NewMail, Set TheMsg =
myInbox.Items.GetFirst should now always find the latest arrived mail
subsequently being used by the userform during the activate event. (I
also toke note of your advice to get and dereference each object
separately

You've been a great help.
Mike
 
I wouldn't always count on it. You might want to use Sort to make sure the
items are in the order you expect.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
Back
Top