Help with simple user form

H

Heather Mills

I would like to learn how to use simple user forms for a front end to
some macros I have written. I would really appreciate having a simple
user form to look at as a template. Would someone be willing to create
one?

Here's a simple form that would be very helpful to me as a template:

This form would be used to set some of the page parameters to position
a text area on a larger page. For example, I might want to position a
text area for a 4x6 card on a letter-size (8.5 x 11) page. It could be
positioned in the upper right corner, centered at the bottom, etc.

The form would have 5 input fields: form height (inches), form width
(inches), horizontal alignment (left, center, right), vertical
alignment (top, center, bottom), and minimum margin (inches). The
minimum margin would default to 0.5. The others would start out blank.
The alignment controls would be radio buttons.

The form would have 2 read-only data fields: page width (usually 8.5)
and page height (usually 11). These would be based on the document
settings.

The form would have 4 output fields (calculated): the 4 margins (left,
right, top, bottom).

The form would have 3 controls (buttons): Calculate (calculate the
margins and display the results), Reset (reset the form to the intiial
values), Cancel (exit without changing any document settings), and Set
Margins (change the document margins to the calculated values).


The form itself would look something like this:

Paper Height (11.0) Paper Width (8.5)

Form Height ________ Align o Top o Center o Bottom
Form Width ________ Align o Left o Center o Right

Minimum Margin ___0.5___

Margins Top ..... Bottom ..... Left ..... Right .....

[Calculate] [Reset] [Cancel] [Set Margins]


The values in () indicate read-only fields.
The ________ fields indicate input fields without default values.
The ___0.5___ indicates an input field with a defrault value.
The [] fields indicate control buttons.

The form also needs a place for an error message such as if the user
enters a form width of 20 or a minimum margin that is too large.

I've read the documentation and several tutorials. I think I have a
general idea how to do this, but there are good ways to write code and
poor ways. I'd like to see one simple user form done with good coding
methods.

Thanks a bunch

PS: I'm posting this here, because there is almost no activity on
microsoft.public.word.vba.userforms. If this is the wrong place,
please advise.
 
H

Heather Mills

Heather, please download http://jay-freedman.info/form_userform.zip and look at the code in that userform. If you have any questions, please post them here.

I downloaded and unzipped it. I got a file named "Form_Userform.dot".

If I double-click on it, I get a Security Warning saying that Macros
have been disabled. I clicked on the Options... button and then the
Enable this content radio button. Then a new document opens but no
user form appears.

I then opened the VBA editor (Alt-F11) and then the Project Explorer
(Ctlr-R) and was able to see the userform and code, but I can't get it
to run.

I'm using Word 2007.

Sorry to be such a dope...
 
G

Graham Mayor

For the macro to run when you open a new document from the template, rename
the macro AutoNew.

As I have mentioned to you in similar earlier threads, I still feel that it
would be simpler to create document templates with the settings
pre-configured for these odd documents than to try and cobble something
together with a macro.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
J

Jay Freedman

Because I wasn't sure what version of Word you have, I created the template in Word 2003. It will work in Word 2007, but there are a couple of differences.

First, to avoid the need to enable macros, you should put the template into the folder that Word want it in, which it considers a "trusted location". If you haven't changed it since installation
(almost nobody does), that folder will be

%appdata%\Microsoft\Templates

(the expression %appdata% automatically points to a location in your personal profile, which depends on your version of Windows).

Second, instead of opening the template itself, you should use the New command (on the Office button) to create a document based on the template. You'll still be able to open the code in the macro
editor, but you won't risk having the macro change the margins of the template itself.

Third, Graham is correct that you can (in the macro editor) go to Module1 and rename the SizeForm() macro as AutoNew(). Then the userform will appear each time you use the New command to make a
document based on the template. In the meantime, there is a button to run the SizeForm() macro; you'll find it on the Add-Ins tab of the ribbon. (In Word 2003, this button would appear on a floating
custom toolbar, but Word 2007 doesn't allow such toolbars.) After you rename the macro, the button won't work any more but you won't need it.
 
H

Heather Mills

Because I wasn't sure what version of Word you have, I created the template in Word 2003. It will work in Word 2007, but there are a couple of differences.

First, to avoid the need to enable macros, you should put the template into the folder that Word want it in, which it considers a "trusted location". If you haven't changed it since installation
(almost nobody does), that folder will be

%appdata%\Microsoft\Templates

(the expression %appdata% automatically points to a location in your personal profile, which depends on your version of Windows).

Second, instead of opening the template itself, you should use the New command (on the Office button) to create a document based on the template. You'll still be able to open the code in the macro
editor, but you won't risk having the macro change the margins of the template itself.

Third, Graham is correct that you can (in the macro editor) go to Module1 and rename the SizeForm() macro as AutoNew(). Then the userform will appear each time you use the New command to make a
document based on the template. In the meantime, there is a button to run the SizeForm() macro; you'll find it on the Add-Ins tab of the ribbon. (In Word 2003, this button would appear on a floating
custom toolbar, but Word 2007 doesn't allow such toolbars.) After you rename the macro, the button won't work any more but you won't need it.

Jay, thanks so much for all your work. Now I've got some work to do.

The macro that calculates the margins is working perfectly. I am able
to print on any size form and know exactly where the text will be. I
used to waste half my special paper just getting things positioned
right. Right now, I am setting constants in the macro. As soon as I
get this userform working, I'll be able to enter the parameters
without editing the macro.
 
H

Heather Mills

For the macro to run when you open a new document from the template, rename
the macro AutoNew.

Ok, thanks.
As I have mentioned to you in similar earlier threads, I still feel that it
would be simpler to create document templates with the settings
pre-configured for these odd documents than to try and cobble something
together with a macro.

And I heard you each time. I agree that it would be simpler if I just
had a few to do or even if I had a lot to print but only using a few
forms.

My situation is different. There are a couple of reasons that I really
need the macro and the userform.

1. I frequently need to print on these odd forms. I often buy special
cards for the grandkids and want to use a funny font or include some
funny graphics. These cards are all diffrenet sizes and shapes.

2. The calculations are not difficult, but they are easy to get
backwards. Computers are good at doing the same thing in the same way.
If I get the macro debugged, it will reliably calculate the margins.

3. I have 2 printers and they print odd forms differently. One feeds
the paper top edge first and only the left paper guide moves. This
means that the text for the custom form needs to be position in the
upper left corner of the letter-size page. The other printer feeds the
paper right edge first and the paper guides both move together,
centering the form. This means that text must be positioned against
the right edge and centered vertically. So, unless I'm going to have a
template for each form for each printer, templates are not a good
soluition.

4. Many times, it's just one card. I don't want to make a template for
a form that I'll never use again. And, I have to get it right the
first time, because I only have one card. I've been doing this for
years and I have done the calculations wrong a few times and then hjad
to go back to the store to buy another card.

5. I will make templates for the forms that I use repeatedly, like my
personal stationery. But my macro will make creating those templates
much easier and more likely to be correct.

6. I am planning on getting a new color printer. If it feeds the paper
differently, I'll need new calculations. One of the features I want to
add to my macro is Named Printers. I can define a printer and save the
paper feed parameters. The macro will then know how to calculate the
margins. If I change printers, I don't need to reenter the paper
specs.

Now let me ask you a question: Why can't you just accept that this is
how I want to do it and stop trying to convince me that you know what
I need better than I do? ;-)

When I get this all working, I'll post the finished product. Then you
can tell me if it's worth anything. I'm sure you'll have an opinion.
;-)

Cheers
 
G

Graham Mayor

Now let me ask you a question: Why can't you just accept that this is
how I want to do it and stop trying to convince me that you know what
I need better than I do? ;-)

If I didn't accept it I wouldn't have been helping you with producing the
code. :)
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top