Automatically update tracking number on form

I

isaia

I need to create a form that contains a tracking number; that is, a number on
the form that increments sequentially each time the form is completed by a
new user. Is there any way to do this using Word 2003?
 
I

isaia

Thanks, Jay.

I am a novice where unrecorded macros are concerned. In running this macro,
I get the error: "Variable not defined." I am able to run the macro if I
delete the line "Option Explicit". Is the deletion of this line an
appropriate action?
 
J

Jay Freedman

Hi Isaia,

Deleting the "Option Explicit" is allowable but not the best idea.

To understand why, you should understand the idea of declaring variables in a
macro. To declare a variable, you include a line of code that starts with the
keyword Dim (short for "dimension", an old programming term), then has the
variable's name, and then the data type of the variable -- whether it's an
Integer or a String or whatever. Giving a declaration like this lets VBA check
for incorrect uses of the variable, which helps in debugging.

If the "Option Explicit" is included, that tells VBA to check every variable
used in the macro to be sure it's declared. If a variable isn't declared, VBA
will show the error message you saw.

If the "Option Explicit" isn't included, then VBA won't check for declarations.
It then assumes that any variable it hasn't seen before has a data type of
Variant and an initial value of 0. If the "new" variable name really should be
an old variable name but you made a typographical error, too bad -- you have a
bug, and it may be hard to find.

The article http://www.word.mvps.org/FAQs/MacrosVBA/DeclareVariables.htm
explains this in some more detail.

The unfortunate fact is that the first article I sent you to doesn't contain a
declaration for the variable named Order. If you want to keep the "Option
Explicit" but not get an error, insert this line after the "Sub AutoNew()" line:

Dim Order As Variant
 
I

isaia

Hi Jay,

Thanks for the great explanation on declaring variables. I think I'm
getting this macro-writing stuff little by little! Can I impose on you to
answer one more question? It's macro-related but beyond the tracking number
question.

I have been successful in incorporating the macro code to add a tracking
number to my Word documents. (Thank you!) The last step I'd like this macro
to perform is to open the SaveAs dialog box. I have tried the SaveAs method
by following the Visual Basic Help examples but, as I'm sure you are aware,
it performs the function and closes the dialog box. Is there any way open
the Save As dialog box and keep it open, allowing the users to fill in their
desired information and click the Save button themselves?
 
J

Jay Freedman

Hi Isaia,

Yes, you can present the dialog for the user to fill in. The general ideas you
need are these:

- All the built-in dialogs are members of the Dialogs collection, and there are
predefined constants for most of them. For example, the Save As dialog is

Dialogs(wdDialogFileSaveAs)

- That expression represents a Dialog object. Each Dialog object has several
useful methods. The one you want is the .Show method,

Dialogs(wdDialogFileSaveAs).Show

That puts the dialog on screen, and when the user fills in the boxes and clicks
the Save button, it actually does the save. The other methods are .Display
(which shows the dialog and gathers the user's entries, but returns them to the
macro without doing the dialog's action) and .Execute (which does the action
without showing the dialog on screen).

- The Dialog object also has properties that represent the boxes and other
controls in the dialog. These are different for each dialog. To find out about
them, see http://www.word.mvps.org/FAQs/MacrosVBA/WordDlgHelp.htm. This is
probably not useful for you in the macro you're writing now, but if you ever
want to direct the file to a particular folder, you can do that as shown in
http://www.word.mvps.org/FAQs/MacrosVBA/ChangeSaveAsPath.htm.
 
I

isaia

Hi Jay,

Great! This worked perfectly. Thanks again for all your help and for the
references to mvps.org's great information.

I've learned a lot with this exercise and next time, I hope my questions
will be more challenging for you!
 

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