How do I . . .

D

Dodger Jim

I am a former WordPerfect user. In WordPerfect I wrote a macro that was run
from a toolbar button. This macro brought up a menu of names for the user to
choose from; then a second menu to choose a second name. The macro then
formatted a memo document with the first name in the TO: postion of the memo
and the second name in the FROM: position of the memo. I am not sure how to
accomplish the same end result within Word. Can someone help me get started?
I have some knowledge of VB 6 but not much on VBA. What does VBA use where
VB uses a form on which to place objects and controls?
 
D

Dawn Crosier

Dodger Jim,

I too am a Former WP user. What you will want to do in Word is create a
UserForm. On the UserForm, you will want to put in two combo boxes. You
will populate the combo boxes with your lists of names. Then when your
users click the OK button, you can take the value out of the combo boxes and
paste that into your spots on your Memo document. Create Bookmarks where
you want the information to be placed.

Creating a UserForm in Word, is very similar to one in VB. Inside the VBE
(ALT+F11). Insert, UserForm. The control toolbox should look familiar to
the one you are used to working with in VB6. I suspect that you will
recognize many of the commands as well.

Below are two different methods to loading your combo boxes and getting the
info into your Word document. The code you will want to associate to your
keystroke command to open the form, is

Sub AddressMemo()
frmAddress.Show 'Use the name of the form you created. If you do not
rename it, call it UserForm1
End Sub

Post back if you need more help.

*************Code Sample Start*********************************

This routine loads a listbox (or a combobox) with client details stored in a
table in a separate document (which makes it easy to maintain with
additions, deletions etc.), that document being saved as Clients.Doc for the
following code.



On the UserForm, have a list box (ListBox1) and a Command Button
(CommandButton1) and use the following code in the UserForm_Initialize() and
the CommandButton1_Click() routines



Private Sub UserForm_Initialize()

Dim sourcedoc As Document, i As Integer, j As Integer, myitem As Range, m As
Long, n As Long

' Modify the path in the following line so that it matches where you saved
Suppliers.doc

Application.ScreenUpdating = False

' Open the file containing the client details

Set sourcedoc = Documents.Open(FileName:="e:\worddocs\Clients.doc")

' Get the number or clients = number of rows in the table of client details
less one

i = sourcedoc.Tables(1).Rows.Count - 1

' Get the number of columns in the table of client details

j = sourcedoc.Tables(1).Columns.Count

' Set the number of columns in the Listbox to match

' the number of columns in the table of client details

ListBox1.ColumnCount = j

' Define an array to be loaded with the client data

Dim MyArray() As Variant

'Load client data into MyArray

ReDim MyArray(i, j)

For n = 0 To j - 1

For m = 0 To i - 1

Set myitem = sourcedoc.Tables(1).Cell(m + 2, n + 1).Range

myitem.End = myitem.End - 1

MyArray(m, n) = myitem.Text

Next m

Next n

' Load data into ListBox1

ListBox1.List() = MyArray

' Close the file containing the client details

sourcedoc.Close SaveChanges:=wdDoNotSaveChanges

End Sub



Private Sub CommandButton1_Click()

Dim i As Integer, Addressee As String

Addressee = ""

For i = 1 To ListBox1.ColumnCount

ListBox1.BoundColumn = i

Addressee = Addressee & ListBox1.Value & vbCr

Next i

ActiveDocument.Bookmarks("Addressee").Range.InsertAfter Addressee

UserForm2.Hide

End Sub



The Initialize statement will populate the listbox with the data from the
table and then when a client is selected in from the list and the command
button is clicked, the information for that client will be inserted into a
bookmark in the document. You may want to vary the manner in which it is
inserted to suit our exact requirements, but hopefully this will get you
started.



To make it easy for you, the code has been written so that it will deal with
any number of clients and any number of details about each client. It
assumes that the first row of the table containing the client details is a
header row



Or, you can code the information directly in the Form itself: Second
Example ****



Private Sub UserForm_Initialize()

On Error GoTo EH

Dim ClientList(2)

cboClient.ColumnCount = 1



'Load PracticeArray

ClientList(0) = "Susie Smith"

ClientList(1) = "Joe Johnson"

ClientList(2) = "Steve Andrews"



'Load data into combo Box

cboClient.List() = ClientList

Exit_EH:

Exit Sub



EH:

MsgBox Err.Number & ": " & Err.Description

Resume Exit_EH



End Sub



Then to insert the values into your document you could use this code:



Private Sub cmdOK_Click()

ActiveDocument.Bookmarks("To").Range = Me.cboClient.Value

frmAddress.Hide

Unload frmAddress

End Sub








**************Code Sample
END***********************************************************
--
Dawn Crosier
Microsoft MVP
"Education Lasts a Lifetime"

This message is posted to a newsgroup. Please post replies and questions to
the newsgroup so that others can learn as well.
 
D

Dodger Jim

Dawn,
Thank you so much for your detailed guidance! I will probably need more
help at some point, but this seems like it will get me started. Thanks,
again!!!

DodgerJim
 
D

Dodger Jim

Dawn,
After reading a number of times what you wrote for me, I still found myself
lost as to where to actually start. This is my first attempt at a macro of
any kind in Word and I can't seem to grasp how the document makes connection
with the macro (the code and UserForm)? What exactly do I do first? Create
the document? And then, when I start writing code, what do I write first?

Thanks again for all your help! I wish I could say with some confidence
that I won't have more questions, but I know better than that!

Dodger Jim
 
C

Charles Kenyon

See http://word.mvps.org/FAQs/Userforms/CreateAUserForm.htm.

A document is created based upon a template. That template is normal.dot
unless you specify something else. The document maintains an attachment link
to the underlying template and has access to the code and forms in that
template, unless you change the attachment.

To create a UserForm I would first create a template and then put the code
and UserForm in the template. You could trigger the UserForm with an AutoNew
macro in the template.
--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide

See also the MVP FAQ: http://www.mvps.org/word which is awesome!
--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
D

Dodger Jim

What is an 'AutoNew macro'? How do I create one?

Why doesn't the help file in VBA list this term? I'm finding the help file
in VBA pretty useless most of the time! It is frustrating!

I thought the work I had done in VB would assist me when I got into VBA but
it hasn't seemed to work out that way so far. I feel like a complete
beginner. Every time I get an answer to a question, there's another term I'm
not familiar with and can't find a definition for it. Is there a better
source of help for VBA other than within VBA/Word?

Thanks for your patience!
 
J

Jonathan West

Dodger Jim said:
Dawn,
After reading a number of times what you wrote for me, I still found
myself
lost as to where to actually start. This is my first attempt at a macro
of
any kind in Word and I can't seem to grasp how the document makes
connection
with the macro (the code and UserForm)? What exactly do I do first?
Create
the document? And then, when I start writing code, what do I write first?

Thanks again for all your help! I wish I could say with some confidence
that I won't have more questions, but I know better than that!

Hi Jim

This article may help you

What do I do with macros sent to me by other newsgroup readers to help me
out?

http://www.word.mvps.org/FAQs/MacrosVBA/CreateAMacro.htm


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
C

Charles Kenyon

An AutoNew macro is a macro named "AutoNew" in a template. It runs whenever
a new document is created based on the template. Others are AutoOpen and
AutoClose. http://word.mvps.org/FAQs/MacrosVBA/DocumentEvents.htm

See http://word.mvps.org/FAQs/MacrosVBA/VBABasicsIn15Mins.htm. While a
knowledge of vb is helpful, it isn't sufficient.
--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide

See also the MVP FAQ: http://www.mvps.org/word which is awesome!
--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
D

Dodger Jim

I have a template named Memorandum.dot in which I am writing a my macro. If
I try to save from within the VBE, it gives me a pop-up saying I'm trying to
save to the normal.dot and it is read-only. What does this mean? When I
save from within the document portion of Memorandum.dot, I do not get the
pop-up. Does this save the code I've written in the VBE as well?

What is the difference between the Normal.dot and the Memorandum.dot that I
have created? There seems to be two different usages for this term
"template" - is there? If so, can you explain them and how I differentiate
between them, please?
Thank you!
 
D

Dawn Crosier

Jim -

When you are looking inside the VBE window, you should see several windows.
There is one which is typically located on the left which is the Project
Explorer. If you will turn that on, I think things will start explaining
themselves to you. (From the View menu, select Project Explorer - or CTRL
+R). If you have your Memorandum.dot template open, you will see it listed
as well as the Normal.dot project. If you simply record a macro using the
recorder, the macro will default to the Normal.dot project. Word sets it up
so that if a Module is not already created, then Word will create a new
Module (container) to hold the macro. It is called NewMacros by default.

All documents / templates are always based on the Normal.dot template at
sometime. Once you create a new template, then new documents can be based
off of them. Think in terms of a piece of paper that you have drawn on. The
paper is always there. When you take the paper to the copier, and make a
copy, the new copy has the drawing on it. Now it is a separate piece of
paper that you can continue to draw on, but the base paper is always there.
You can make new copies of the new drawing (Which would be another template)
or you can revert to the original copy (Memorandum template)

The code that I gave you should sit behind the UserForm that you need to
include in the Memorandum template. So, make sure you have the Memorandum
Project selected in the Project Window, and from the Insert Menu, select User
Form. Once you have the UserForm displayed, the control toolbox will
display, and placing objects on the form is just like VB.

In the project window, you will see a folder called Forms, which when you
expand it, you will see the Form you just created. To modify how the FORM
itself works (Caption, Name, Scroll Bars, etc.) then you will want to display
an additional window called the Properties window. (View, Properties Window
- or ALT + F4). The properties window will also allow you to set the
properties associated with the various controls which you have placed on the
form. Select the Combo Box, and the properties window will display those
properties available to you for the combo box. To start, leave all the
properties set to default EXCEPT the name of the object. ComboBox1 is not
very descriptive. <smile>

Once you have your OK button on your form, if you double click on the
button, it will take you to the code window for the button itself. Your
cursor will be defaulted to the OnClick event for the button.

To test your form, you can make sure that your form is in view, and press
the F5 (Run) key, or on the standard toolbar you will see a right pointing
triangle which will allow you to run the form. At that point you can test to
see whether your combo boxes have properly initialized by seeing whether you
have values in them.

Once you save your Memorandum template, your form & code will be saved as
well. However, depending on the version of Word you have and the Security
Level you have Word set to, you may have trouble re-opening your template and
getting your code to run again. If you set your security down to Medium,
when you open the template, or base a new document on the template, you may
be prompted to enable the macros. Once you get things going in development,
if you are going to share with others, you may want to consider signing your
code so that once your users have "trusted" you, they will no longer get the
question even if security is set to High.
 
D

Dodger Jim

Thanks, Dawn! This helped a great deal!

One more "small" problem. I want this macro to run when I open this
document. I can not figure out the right code to put into the Private Sub
Document_Open() routine to get the code to run when the template document is
opened. Am I correct in having the Private Sub Document_Open() routine
behind the document and not behind the UserForm?

Thanks for all your help!
 
C

Charles Kenyon

See http://word.mvps.org/FAQs/MacrosVBA/DocumentEvents.htm. You also could
make it an AutoOpen macro in a module of your template.
--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide

See also the MVP FAQ: http://www.mvps.org/word which is awesome!
--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
D

Dodger Jim

It still won't run when I open the document. The only code I have in the
AutoOpen() macro is Load frmNewForm. It acts like it doesn't recognize the
Load command. The only way I can get the macro to run is to be in the VBE
window, the code window behind my UserForm. When I click on the Run icon, it
runs fine.

Thanks for your patience!
 
J

Jonathan West

Dodger Jim said:
It still won't run when I open the document. The only code I have in the
AutoOpen() macro is Load frmNewForm. It acts like it doesn't recognize
the
Load command. The only way I can get the macro to run is to be in the VBE
window, the code window behind my UserForm. When I click on the Run icon,
it
runs fine.

No, that's the wrong code. Instead, use this

frmNewForm.Show


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
D

Dodger Jim

Nope. That doesn't do it either. So that I'm clear in what I have, I have
Module1 with the AutoOpen() macro containing one line of code:
frmNewName.Show as you said. In the code VBE behind the UserForm, I have two
events. I have the Private Sub UserForm_Initialize() which only contains a
series of statements that populate two list boxes using the .AddItem method.
The second event is a click event on a command button that takes the choices
made from the list boxes and places them in the template document at the four
bookmarks I have established. The code in the UserForm runs great and work
correctly when I Run it from the VBE window. I just can't get it to run
automatically when the template document is opened.

Thank you, again!!!
 
C

Charles Kenyon

What happens if you run your AutoOpen macro directly in the vbe editor? (F5)
--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide

See also the MVP FAQ: http://www.mvps.org/word which is awesome!
--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
J

Jonathan West

Dodger Jim said:
Nope. That doesn't do it either. So that I'm clear in what I have, I
have
Module1 with the AutoOpen() macro containing one line of code:
frmNewName.Show as you said. In the code VBE behind the UserForm, I have
two
events. I have the Private Sub UserForm_Initialize() which only contains
a
series of statements that populate two list boxes using the .AddItem
method.
The second event is a click event on a command button that takes the
choices
made from the list boxes and places them in the template document at the
four
bookmarks I have established. The code in the UserForm runs great and
work
correctly when I Run it from the VBE window. I just can't get it to run
automatically when the template document is opened.

Thank you, again!!!

Actually, looking back at your original post, i think you need to use the
AutoNew macro, not AutoOpen.

AutoOpen runs when you open a document based on a template. Autonew runs
when you create a new document beased on the template.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
D

Dodger Jim

Not having any luck at all with this! Running AutoOpen or AutoNew either one
from the VBE works fine. But still, nothing happens when I close the
document and open/retrieve it again. Is there a setting somewhere that tells
Word to recognize the AutoOpen or AutoNew upon opening a file? I'm so close
on this but just can't get it.

Another question has cropped up. How do I protect the document template so
my users see only the document that they are going to type in and are
required to save it to a different name?

Thanks!
 
C

Charles Kenyon

How do I protect...

(1) make it read only.
(2) You should be using a template, not a document. The proper way to use a
template is to store it in a templates folder and use File > New to generate
a new document based on the template. That document is named when it is
saved and is saved somewhere other than a templates folder. For more on the
different kinds of templates, tabs on the file new dialog, and locations of
templates folders see http://addbalance.com/usersguide/templates.htm.

Let's start with basics, getting away from your specific code.

Rename your macro to any name you want that does not have an Auto prefix.

Create a new macro as follows in your template:

Sub AutoNew()
MsgBox "This is the AutoNew Macro"
End Sub

In the vbe editor run this macro. Does it display the message box? If so,
then save your template and create a new document based on the template.
Does the macro produce the message box when it runs?

If it does, rename your macro AutoNew and try again.
 

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