Results 1 to 11 of 11
  1. #1
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Global variables (WinNT4, Word97)

    Please could someone tell me if there is a way of getting VBA to remember a variable after a sub has finished? I need to use the information in another sub in another project and can't find a way to get it to remember it long enough to use it in the new sub!

    Many thanks in advance
    Beryl M


  2. #2
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Global variables (WinNT4, Word97)

    The obvious way is to declare it as a public variable outside of any sub or function.
    <pre>Option Explicit
    Public strRememberMe As String

    Sub Test()
    strRememberMe = "Hello World"
    End Sub

    Sub Test2()
    MsgBox strRememberMe
    End Sub
    </pre>

    Alternatively you could store the information in a Document variable, or in the Registry, or...

    StuartR

  3. #3
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Global variables (WinNT4, Word97)

    Hi Stuart, yes, I had declared it outside the subs/functions, and as a 'Public', as you suggested. But the next sub is in a completely different project and it forgets the content by that time.

    Putting it in the Registry seems a bit extreme to me, apart from the fact that this code is going to be run from over 250 PCs and I have no way of putting anything in every Registry!

    However, a Document variable sounds interesting - what are they, please? They aren't mentioned anywhere I can see in the VBA help ...

    Many thanks
    Beryl M


  4. #4
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Global variables (WinNT4, Word97)

    I have just tried this with the two subs in a different module to the declaration of strRememberMe and it works fine. You need to be careful if you run this code in the de######, because clicking on the "Stop" button will clear all global variables of this type.

    For information about document variables look in the VBA help for information about the Variables collection, you typically use this to store information about the document that you need when Macros are running but you don't want the user to change (or even see).

    Typical code might be

    ActiveDocument.Variables.Add Name:="SRTemp", Value:="Remember This"
    or
    For Each varDocVar In ActiveDocument.Variables
    If varDocVar.Name = "SRTemp" Then strStatus = varDocVar.Value
    Next varDocVar

    StuartR

  5. #5
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Global variables (WinNT4, Word97)

    Thanks Stuart - although I had checked out the Variables bit in VBA help and didn't see anything re document variables!

    I shall try your code and see what happens!
    Beryl M


  6. #6
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Global variables (WinNT4, Word97)

    Just thinking about this one, could it be something to do with the fact that the first sub runs a template, so the active document is changing? and if so, is there an application variable like the document variable?
    Beryl M


  7. #7
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Global variables (WinNT4, Word97)

    A global variable is an application variable. How sure are you that the global variable is getting cleared during normal running? I have only seen this happen when the programmer is using the VBE.

    StuartR

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

    Re: Global variables (WinNT4, Word97)

    Stuart,

    Beryl has a valid point, I believe. A global variable is global to a project, i.e. a document. If you define and set a global variable in DocumentA, it is known in every module in DocumentA, but it is not accessible in modules residing in DocumentB, unless I am mistaken. So if you use code in DocumentA to create a new document DocumentB, a macro in the template attached to DocumentB can't see a global variable defined in DocumentA.

    Document variables are better suited for this, because you can specify the document.

  9. #9
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Global variables (WinNT4, Word97)

    Well, I tested it by adding a line at the top of the second sub saying 'msgbox "templatename = " + templatename' and it crashed, saying the variable wasn't defined. I have, of course, checked that I hadn't mistyped the variable name or anything like that.

    What I am trying to do is to create a master list of all templates, with links to master copies of them, which I am doing by using macrobuttons. However, to create the master copies, I have to run the templates, and then I have another macro (in my startup template) which sets up the master copy in the format required, including putting the template name in the document. I had been doing this by picking up the attachedtemplate name, but far too many of them have been coming up as the attachedtemplate being 'normal' (!) and I was getting fed up with having to change it all the time, so since the first macro (that creates the doc from the template) picks up the template name to run in a variable, I thought I would just use the content of that variable to get the template name.

    Not having much success at the moment!
    Beryl M


  10. #10
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Global variables (WinNT4, Word97)

    Beryl,

    One way to do this would be to have the global Public variable reside in the global template that is in the startup directory, and then set a reference to this global template, in each of the other 'attached' templates' code projects. That way the value of the global variable in the global template will always be available to each individual template's project. I've seen caveats that global variables like this should not be overused, but in a situation like you describe it may be the only workable solution.

    Gary

  11. #11
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Global variables (WinNT4, Word97)

    That sounds good - I'll try it!

    Ta muchly <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>
    Beryl M


Posting Permissions

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