Re: Variables and scope ? !!!!



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

"Jezebel" <warcrimes@xxxxxxxxxxxxxx> wrote in message
news:OlkrJmbvFHA.2292@xxxxxxxxxxxxxxxxxxxxxxx
> 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:e5FPiVWvFHA.2312@xxxxxxxxxxxxxxxxxxxxxxx
>>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
>>
>>
>>
>>
>>
>>
>
>


.



Relevant Pages

  • Re: Variables and scope ? !!!!
    ... global -- available to all code in the application, declared in the header ... Declare the variable, in the header of an ordinary module ... Global variables are, in general, bad practice. ... > value that i would use the "const" ...
    (microsoft.public.word.vba.general)
  • Re: static library problems
    ... Do not declare variables in header files. ... variable itself must be declared in a .cpp file. ... >If i want to use this string in other modules, i just need include the header, i need not link this static lib. ...
    (microsoft.public.vc.mfc)
  • Re: Exported function mangaled name
    ... extern "C" { ... You have otherwise defined a function unrelated to the header ... You have to declare the function the same way in your .cpp file as in your .h file ... #define LIBSPEC __declspec ...
    (microsoft.public.vc.mfc)
  • Re: Interpretation, please... (Declare Sub)
    ... I love this VBA stuff, but feel like I'm crossing the ocean on a few little ... The Declare keyword says you're going to refer to a routine that's ... in a "dynamic link library" (DLL). ... Sub or a Function -- a Function returns a value, ...
    (microsoft.public.word.vba.beginners)
  • Re: Mixed mode woes (hopefully I simply dont get it )
    ... My understanding is that the gcroot template requies managed support, ... It has to be an unmanaged class so that the existing unmanaged ... >> classes can use it and include it's header. ... >> cant declare the managed instance variable in the header because it is ...
    (microsoft.public.dotnet.languages.vc)

Loading