| 12. Finally let's
alter the original macro provided above to call and use the UserForm data
instead of the standard message box:

Technical details: The
first 11 steps create the definition of frmCustomMsgBox, which is a
"pattern" for a UserForm. Strictly speaking a UserForm is a
user-defined data type.
In step 12, the calling macro is
provided with all the necessary code to create, load, show, utilize data,
unload and ultimately kill the instance of the UserForm:
- Dim myForm As frmCustomMsgBox
At this point, myForm is
"Nothing." However, in its nothingness it is still a
declared, empty, unloaded, and
undefined object variable of the type "frmCustomMsgBox"
- Set myForm = New
frmCustomMsgBox
This creates
the actual object and loads myForm with a complete working instance of
frmCustomMsgBox. However, the object is not displayed; it is just defined
and loaded.
- myForm.Caption = oWord &
"starts with ""z"""
As the form is now loaded, you
can alter its properties at will. In this line, the calling macro
provides the text for the .Caption property of the UserForm.
- myForm.Show
This puts the form on the screen
in all its glory. The form dialog box is the active window. If the form is
modal (default), some user action (i.e., clicking a button on the form) is
required to hide or destroy the form before any more code in the calling
macro will be executed.
Within the UserForm object there
is a built-in string property ".Tag" to which you can assign a value.
When the user clicks a button,
the _Click function belonging to that button gets called. In the _Click
function, a value is assigned to the .Tag property and then the .Hide method
is called.
The .Hide method removes the
UserForm from the screen and returns control to the calling macro at the
line after .Show. That line grabs the value of the UserForm's .Tag property
and uses it in the logic process.
- Unload myForm
This performs a pure technical function that
triggers the UserForm _QueryClose and _Terminate events. The
_QueryClose event provides data that assess how
a UserForm was actually closed. The _Terminate event "kills" the
UserForm and releases memory previously allocated to the UserForm to the
pool of available memory.
And finally
- Set myForm =
Nothing. Could be considered redundant as its function also kills the UserForm and releases memory previously allocated to the
UserForm to the pool of available memory. Some people frown on using
the Unload statement, but without it the _QueryClose event isn't triggered and
as a matter of personal choice I always use both statements. |