First: constants and variables are different beasts. Constants are a
shorthand way to write literals into your code, to improve readability and
maintainability. You can use them only for simple data types (strings,
longs, etc --- not objects). Eg ---
Const PI as double = 3.14159
then later in code you can use 'PI' in expressions ...
Area = pRadius * pRadius * PI
Second: scope is a separate issue. Your constants and variables can be
global -- available to all code in the application, declared in the header
of an ordinary module with 'Global' or 'Public'; or they are module level --
declared in the header of any code module, optionally with 'Private'; or
they are procedure level, declared within a procedure.
Third: to create a global reference to an object you need two pieces of
code --
a) Declare the variable, in the header of an ordinary module
Public gtblEmployees as Word.Table
b) Set the variable within a function
Public Sub AutoOpen()
on error resume next
Set gtblEmployees = ActiveDocument.Tables(1)
err.clear
End Sub
Note that the variable is cleared if you reset the code project, which
happens automatically if you edit any code in the project.
Fourth: Global variables are, in general, bad practice. They are a prime
cause of bugs, and you hardly ever need them anyway. In this case for
example, what happens if the table gets deleted? Or another table gets
inserted before it? etc
>I wonder if some one can clarify if it is possible
>
[quoted text clipped - 13 lines]
>
> many thanks
Nick Calladine - 20 Sep 2005 15:19 GMT
Hi Jezebel
Thanks for the reply it was very informative
I do understand your point and see how i should do something like this.. it
just my document a protected form and I have controlled the delete /
insertion routine to basically never remove / create afirst table.
So would you still suggest i should not define the table as global
variable.. and do it within each sub as when required... I know it hard
without seeing the project but i no professional vba programmer but would
like to learn good practices and this project is just a simple table
caculation but the data is always being scanned for validation rules and
also changes so i seemed to be using the with activedocument.tables(1) alot
in my code and it was if i ever had change the code i though a global
variable and name would make the code more readable.. and if i was doing a
large project (many many years from now ... lol) would this be a better way
of coding to be able change one variable and thus update my code...
The other thing i am doing at the moment is writing a lot of the code in non
module form and only adding functions to modules... should all code be
written in to a module or in the thisdocument area
At present my code is triggered from a toolbar...and user input is directly
in to a protected document ... should my click code be triggered this way...
Finally do have any good site or books you can recommend ... i getting a
copy of vba for dummies to go through any other suggestions for reading and
learning techniques
Many thanks
Nick
> First: constants and variables are different beasts. Constants are a
> shorthand way to write literals into your code, to improve readability and
[quoted text clipped - 53 lines]
>>
>> many thanks
Jonathan West - 20 Sep 2005 17:12 GMT
Hi Nick
Start out by taking a look at the following articles
Getting To Grips With VBA Basics In 15 Minutes
http://www.word.mvps.org/FAQs/MacrosVBA/VBABasicsIn15Mins.htm
Creating a macro with no programming experience using the recorder
http://www.word.mvps.org/FAQs/MacrosVBA/UsingRecorder.htm
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
Also, be aware that trying to read a post that is a "stream of
consciousness" with no punctuation or uppercase characters is quite hard
work. The people who answer questions here are all unpaid volunteers. It is
in your interest to make them more willing to answer your question, rather
than passing over it and going on to the next one that is easier to read.

Signature
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
> Hi Jezebel
>
[quoted text clipped - 87 lines]
>>>
>>> many thanks
Greg - 20 Sep 2005 18:03 GMT
"Also, be aware that trying to read a post that is a "stream of
consciousness" with no punctuation or uppercase characters is quite
hard
work. The people who answer questions here are all unpaid volunteers.
It is
in your interest to make them more willing to answer your question,
rather
than passing over it and going on to the next one that is easier to
read."
Hear, hear!!