VBA Code Help - Creating a Dictionary

D

Dan

This is what I am looking to do:

I am trying to create a dictionary with my own temrs and
definitions. I have created a "Definition" document that
contains a 2-column
table. First column = Word, second = Meanings/Definitions.

I am trying to create a Macro that will:
1. Ask user which word they want to look up (via an input
box);
2. Open the definition document;
3. Look up the word in the table in the definition
document;
4. If the word is not found, warn user (message box) and
and let the user start over (with the first input box)
5. If the word is found, pick up the definition in the
adjacent cell;
6. Close the definition document;
7. Display the definition (with a modeless display??)

I don't know a lot about VBA, so I have been having some
problems trying to write this code. Could someone post a
code like this or take time to help me out? If you could,
that would be GREAT.

Thanks!

Regards,
Dan
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Dan > écrivait :
In this message, < Dan > wrote:

|| This is what I am looking to do:
||
|| I am trying to create a dictionary with my own temrs and
|| definitions. I have created a "Definition" document that
|| contains a 2-column
|| table. First column = Word, second = Meanings/Definitions.
||
|| I am trying to create a Macro that will:
|| 1. Ask user which word they want to look up (via an input
|| box);
|| 2. Open the definition document;
|| 3. Look up the word in the table in the definition
|| document;
|| 4. If the word is not found, warn user (message box) and
|| and let the user start over (with the first input box)
|| 5. If the word is found, pick up the definition in the
|| adjacent cell;
|| 6. Close the definition document;
|| 7. Display the definition (with a modeless display??)
||
|| I don't know a lot about VBA, so I have been having some
|| problems trying to write this code. Could someone post a
|| code like this or take time to help me out? If you could,
|| that would be GREAT.
||

Like I wrote in the docmanagement group, look up the resources Suzanne and I
mentioned, also, Google is definitely your friend.

Then do it step by step. The steps I suggested are not necessarily the ones
you are going to end up with. It was more of a framework to get you going.
In any case... try to do step 1. If you cannot or run into problems, post
your code and many people will be happy to assist. As it is now, unless
someone has written something to fit the exact same requirements you have,
you are going to get code that sorta fits your requirements, but you will
not be able to adjust it as you do not know about VBA.
On the other hand, if you try writing it step by step, you will learn about
VBA and you will also understand the suggestions people will post regarding
any code you may come up with.

Just to get you going, do ALT-F11 from a Word document, this will open the
VBA editing window. On the left you should see the Project window (a list of
currently opened documents and templates). Find the one with your document
name and look at the tree stemming from it. You will see "ThisDocument"
under "Microsoft Word Objects". This is a special module from which you can
automate events that are linked to creating, opening and closing documents,
amongst others things. Right on that tree and from the contextual menu
choose to insert a standard module. It will, by default, be called "Module1"
and it will be added a new branch called "Modules". You may want to change
that name by selecting Module1 and clicking in the "Name" area in the
property window under the project window (If neither of these windows are
displayed, activate them from the View menu in the VBA menu bar).

Then, to make sure you activate the code windows that goes with Module1,
double click on Module1 (or whatever new name you may have given to that
module). To the right you will see a large white area with two dropdown
lists at the top. Click in that area and type

Sub Test

and hit Enter.

You will see that

End Sub

is automatically added under Sub Test.

This means you now have a macro called "Test" in the module called Module1.

If you were to call this macro from another one in the same project you
would only have to type

Module1.Test

Now, place the cursor between the Sub and End Sub statement and type

inputbox

and hit enter. Notice how "inputbox" is changed to "InputBox". This is
"Intellisense". I use it all the time to make sure that I type the right
words... This means that Word has recognized the code and has given it its
default letter case setting. If that did not happen, it would have meant
that you had misspelled the code.

Finally, select InputBox and hit F1. This will take you directly to the help
topic on InputBox. Read it up and look at the examples. You should be able
to create your own in no time.

Sometimes, you may want to use the macro recorder to record a macro. But
remember that this is just a tool to help you write decent code. The macro
recorder usually writes way too much code and is not always very stable. But
it is a good way to learn key words.
You can test your macro by running it from the code window. F5 will run it
in full. F8 will run it step by step, you have to hit F8 to make the code
execute line by line. Very useful to track changes in the document (you can
switch from the code window to the document window and back if you want) or
to track changes to variables.
When you are finish writing it, you may want to compile it first (Debug
menu). Word will check if there are any inconsistencies or illegal calls.

To run the macro from the document window, do ALT-F8 and find it in the
list.
When you are all done, you will want to save your document as a template
(You may want to do that right from the start) and place it in the start-up
folder so as to have global template that will be available to all
documents. Then you will probably want to create a toolbar button to call
the macro (or even a new toolbar if you end up with lots of macros,
therefore lots of buttons!). When you get to that stage, I am sure you will
easily find help around here!

Good luck!

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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