View Single Post
  #4   Report Post  
Posted to microsoft.public.word.docmanagement
Jay Freedman Jay Freedman is offline
external usenet poster
 
Posts: 9,854
Default Automatically update tracking number on form

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/Macros...eVariables.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


On Tue, 2 Sep 2008 11:43:01 -0700, isaia
wrote:

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?

"Jay Freedman" wrote:

On Thu, 28 Aug 2008 14:52:03 -0700, isaia
wrote:

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?


See http://www.word.mvps.org/FAQs/MacrosVBA/NumberDocs.htm.



--
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.