Variables and scope ? !!!!

N

Nick Calladine

I wonder if some one can clarify if it is possible

I want to set a global or constant in word at the start of a document /
module in vba is this possible

I understand to set a variable i use the "public" and if i require a set
value that i would use the "const"

But how do set a variable to the whole document which refers to a table

something like const tblEmployees As Tables = ActiveDocument.Tables(1)

or this is poor coding or bad practice... ?

if ppl have any tutorials or links which might help it all become clear

many thanks
 
J

Jezebel

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




"Nick Calladine" <n i c k c a l l a d i n e @ n t lw orl d . c o m (remove
all spaces)> wrote in message news:[email protected]...
 
N

Nick Calladine

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
 
J

Jonathan West

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.

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


"Nick Calladine" <n i c k c a l l a d i n e @ n t lw orl d . c o m (remove
all spaces)> wrote in message news:[email protected]...
 
G

Greg

"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!!
 

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