Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / January 2006

Tip: Looking for answers? Try searching our database.

Basic MsgBox-style userform from inside a macro

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
LurfysMa - 03 Jan 2006 00:11 GMT
Can someone outline the basic steps necessary to create a user form
that I can invoke from inside a macro that will look similar to a
MsgBox but where I can define buttons other than Yes, No, Cancel?

How do I create the suer form -- that is, where does it go?

How do I invoke it from the macro?

How do I retrieve the results depending on what buttion the user
selects?

I just need help getting started.

Thanks

Signature

For email, use Usenet-20031220@spamex.com

Jay Freedman - 03 Jan 2006 04:23 GMT
>Can someone outline the basic steps necessary to create a user form
>that I can invoke from inside a macro that will look similar to a
[quoted text clipped - 10 lines]
>
>Thanks

There's an article on the MVPs site,
http://www.word.mvps.org/FAQs/Userforms/CreateAUserForm.htm, but it
includes a bunch of things you don't need for this job. So let's
simplify...

1. In the VBA editor, select your template in the Project pane on the
left and click Insert > Userform. This puts the empty dialog box and a
toolbox in the main window.

2. In the Properties pane (press F4 to show it if necessary), change
the name from UserForm1 to something meaningful. I like to use the
prefix frm for this, so I might call it frmCustomMsg. Also change the
Caption property, which is what appears in the userform's title bar.
Drag the edges of the userform to the shape that will hold the
buttons.

3. If you want some text above the buttons, click the Label item in
the toolbox and then drag a rectangle on the surface of the userform.
Fill in the Text property in the Properties pane.

4. Click the Command Button item on the toolbox and click in the
userform to insert one. Repeat twice more. (Shortcut: double-click the
toolbox item to make it "sticky", then click three times on the
userform to insert three buttons.)

5. Drag the buttons to align them, or select them all and use the
Format > Align and Format > Horizontal Spacing commands.

6. Select the first button. Change its name in the Properties dialog
to something like cmdRandom, and change its Caption property to what
you want the button to say. Repeat for the other two buttons. Also,
when the Cancel button is selected, change its Cancel property to
True, which makes it respond to the Esc key.

7. Click View > Code (or press F7) to open the code pane associated
with the userform.

8. At the top of the window, declare a variable:
      Public ReturnValue As Integer
This is a property of the userform that a macro can read from outside.
The buttons are going to set the value of the variable.

9. Pull down the dropdown that says "(General)" and select one of the
button names. This creates a function that gets called when that
button is clicked (note the "_Click" in the function's name). Repeat
for the other two buttons.

10. Type in code in each function so they look like this (the function
names may be different, depending on what you named the buttons):

   Private Sub cmdCancel_Click()
       ReturnValue = 0
       Me.Hide
   End Sub
   
   Private Sub cmdRandom_Click()
       ReturnValue = 1
       Me.Hide
   End Sub
   
   Private Sub cmdAlternate_Click()
       ReturnValue = 2
       Me.Hide
   End Sub

Each of these sets a unique value of ReturnValue and then hides the
userform. When the userform hides, control returns to the macro that
called the userform.

11. In your original coloring macro, remove the MsgBox handling code.
Replace it with this:

   Dim UFrm As frmCustomMsg
   Dim iOption As Integer
   
   Set UFrm = New frmCustomMsg
   With UFrm
       .ReturnValue = 0
       .Show
       iOption = .ReturnValue
   End With
   
   ' remove userform from memory
   Set UFrm = Nothing

You can now use iOption as you did in your previous version.

Technical details: The first 10 steps create the definition of
frmCustomMsg, which is a "pattern" for a userform. Strictly speaking
it's a user-defined data type. The "Dim UFrm As frmCustomMsg" line
declares UFrm to be a specific object variable of that type, and the
Set UFrm line makes an actual object and assigns it to the UFrm
variable.

Within the object, there's an integer property UFrm.ReturnValue, to
which you assign the value 0. Then you call the .Show method of the
userform, which causes it to appear on the screen. It sits there until
the user clicks a button. At that time, the one  _Click function
belonging to that button gets called (the other two functions don't
get a chance to execute). Inside the _Click function, a value is
assigned to UFrm.ReturnValue, and then the userform's .Hide method is
called.

The .Hide method removes the userform from the screen and returns
control to the macro on the line after .Show. That line grabs the
value from UFrm.ReturnValue and stuffs it into iOption. Finally, you
set UFrm to Nothing (a special value that means "no longer defined"),
which causes the memory used by the userform to be returned to the
pool of available memory.

--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
LurfysMa - 05 Jan 2006 07:36 GMT
>>Can someone outline the basic steps necessary to create a user form
>>that I can invoke from inside a macro that will look similar to a
[quoted text clipped - 15 lines]
>includes a bunch of things you don't need for this job. So let's
>simplify...

... snip ...

Thanks. I got the form working using most of the instructions on the
MVP article. It's slightly different from the procedure you outlined,
but it does work. When I get time, I'll compare the two and see if I
can figure out where they are different and why.

The "little" macro has grown considerably, but I have learned a lot
about how VBA works. I am sure it will serve me well for other macros
I have wanted to tackle.

My form has 3 buttons: Random, Repeat, and Cancel. If the user clicks
the Random button, the macro will select a random color for each
character in the selection up to a limit that is currently set in the
code (3 consecutive characters with the same color). If the user
clicks on the Repeat button, the characters are set to the colors in
the list in order, repeating them over and over.

I have a few more questions:

1. I would like to allow the user to set the maximum number of
consecutive colors in Random mode. What's the best way to handle that?

I was considering adding a TextBox to the form and filling in the
default (3). The user can enter a different number. I would need to do
some checking to make sure it is an integer.

I am not sure how to get the user's value into the main code. Do I
declare a global variable?

Is there a better way?

2. I would like to allow the user to select the colors. What;s the
best way to handle that?

My plan was to put a bunch (6-8) of checkboxes on the form. Initially,
Red and Green would be checked. The user could check or uncheck each
box.

As the code is now, the colors are in a Variant array

  vaColors = Array(wdRed, wdGreen, wdBlue)

One nice feature of this approach is that a color could be included
more than once:

  vaColors = Array(wdRed, wdRed, wdGreen)

This would have the effect in Random mode of having the result
"redder" and in Repeat mode of having the characters be colored red,
red, green, red, red, green, ...

To make that work, I would need to provide another text box and allow
the user to type in a list of colors, whcih I would then have to check
for validity.

Suggestions?

3. Once I get this finished, is there somewhere I can post it? Maybe
at least Peter in New Zealand might be interested in it. Maybe
alt.binaries.freeware or comp.binaries.ms-windows?

How can I extract the code and the form into a separate file?

Thanks. You have been extremely helpful and I have learned a lot.

Signature

For email, use Usenet-20031220@spamex.com

Greg - 05 Jan 2006 13:46 GMT
LurfysMa,

I don't have your complete code and I wasn't following your process.
To be frank I was a bit put off by your rather "pushy" attitude in
another post.  That is behind me and if you are interested, I have a
few tips pages on my website that might illustrate some techniques that
you can use.

As for selecting colors, I have a Find It Toolbar that uses a small
userform to replicate the highlight color dialog.  If users want to
hightlight found text this little userform pops up and they select a
"colored" label to make there choice.  You can download the template
with all the code here:

http://gregmaxey.mvps.org/Find_it_tool_bar.htm

Warning:  I am a mere novice in VBA when compared to a Titan like Jay
Freedman.  As bad as I am now, I was even worse when I created the code
used in that Add-In.  It is replete with the spegetti code Jay warns
of.  However, if you step through using F8 I think you will be able to
see what is going on.

I also have a page that illustrates various methods for passing data to
and from a userform and the calling macro:

http://gregmaxey.mvps.org/UserForm_Pass_Data.htm

I would like to see your complete code and would consider posting it on
my website (credit to you of course).
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.