Results 1 to 3 of 3
  1. #1
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Public variables (Word XP)

    I've been given a malfunctioning Word template to analyze. There are several public variable names which are also declared within procuedures (In a different module). I'm suspicious of this, but the ambiguous declaration doesn't cause a problem when I compile.

    I rarely use Public variablea, and when I do I'm careful... so my experience with problems of this nature is limited. In one module of this template, Public objDocument as Document appears in the Declarations section. Set objDocument is used several times within that module to assign a document object. In another module, Dim objDocument as Document occurs in several procedures. A few other procudures have Set objDocument statements, but no Dim statement. The Public declaration would appear to cover for this.

    I'm not clear why this ambiguous declaration doesn't cause a Compile (or run-time) error. Option Explicit is set in the Declaration section.

    It's my understanding that a Public variable extends to all modules of a project. Why, then, does the declaration of a Public variable by the same name in another module not cause a compile error? I can see the pitfalls that could occur as a result, but why no red flag from VBA?

    Thanks,
    Richard Barrett

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Public variables (Word XP)

    It is allowed to declare a variable of the same name at the module or project level and locally within a procedure or function, although it is not considered good programming practice, because it can easily lead to confusion.

    Consider this simple example:
    <code>
    Public objDocument As Document

    Sub ProcA()
    Set objDocument = ...
    ...
    End Sub

    Sub ProcB()
    Dim objDocument As Document
    Set objDocument = ...
    ...
    End Sub

    Sub ProcC()
    MsgBox objDocument.Name
    End Sub
    </code>
    When ProcA is run, the public variable objDocument will be assigned a value.
    If you then run ProcC, you'll see the name of the document stored in the public variable.
    When you run ProcB, it will use the local instance of objDocument. The public variable is unknown within ProcB and cannot be used there. Any value assigned to objDocument in ProcB is only valid within ProcB. It does not affect the value of the public variable in any way. When ProcB finishes, the local variable vanishes into thin air.
    So if you run ProcC again after running ProcB, the value of objDocument will still be the original value assigned in ProcA, not the value assigned in ProcB.

    As you will understand, it is easy to lose track of what objDocument actually is at any given moment, so it is not a good idea to use the same name for local and global variables. But is it not forbidden.

  3. #3
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Re: Public variables (Word XP)

    Thanks for the explanation. I would have assumed that Public would disallow use in a narrower scope.

    The code I'm working with has On Error Resume Next just before the Set statement in a procedure where the variable is not Dim'd. So if there's a problem with the Set statement, the prior value of objDocument is retained. This appears to be the source of trouble.

    Thanks,
    Richard

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •