Results 1 to 13 of 13
  1. #1
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Storing Values for Word Session

    I need to keep some global variables around for the entire duration of a Word session -- that is, until the user exits Word. What's the best way to do this?

    Right now, I'm keeping the values stored in an .ini file or in some cases I just re-read the registry each time the user runs my document production app.

    Global VBA variables go away as soon as the code finishes so they won't work. But is there some way, some trick maybe that will "persist" these vars? The app does have a toolbar that is always displayed. Is there a way to store variables in a toolbar control to make them global?
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  2. #2
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Storing Values for Word Session

    Hi Kevin,
    Would static variables work in this case? If not, I guess you could use the Tag or Parameter properties of your toolbar controls to store them (not sure if that's considered good practice).
    Hope that helps.
    Regards,
    Rory

    Microsoft MVP - Excel

  3. #3
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Storing Values for Word Session

    Rory,

    I tried Static too, but that didn't work. As the Help says: "...Static statement retain their values as long as the code is running."

    My code stops and there they go...

    But I like your idea of using the tag or parameter properties of the toolbar. I think I read about that somewhere. Good practice or not, I'll investigate. Thanks!
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  4. #4
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Storing Values for Word Session

    Kevin,

    I'm a fan of ini files. I've found them easy to manage, easy to reference and easy to update.

    I use the "PrivateProfileString" functions- I've not had a need to go for API calls.

    I tried using the registry at one stage, but that gave trouble. Our registry file is on the local hard drive, so settings were not retained when logging onto another PC (Not that that's a problem for you). I also found accessing the registry harder to use. It was OK when you were sure that you had a value for a key (from memory), but awkward when you did not.
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  5. #5
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Storing Values for Word Session

    I use INI files. I've just posted the code for them. If you need an example of how to get started, let me know.

    Better yet, send me some rough details on the sorts of values you use, when you store/retrieve them.

  6. #6
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Storing Values for Word Session

    If you use ADO, you can also create a recordset on the fly and persist it to the hard drive as a table type file (or XML, it's up to you). That will even last between sessions of Word, but it does require to to open another ADO recordset when you want to use the values the next time.
    Charlotte

  7. #7
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Storing Values for Word Session

    Geoff,

    That's a good point about the registry not "roaming" with the user. I've not been tripped up by that, I guess, because any changes I make to the registry are universal -- everybody gets them and they are not user specific. As far as accessing the registry being harder to use, I've not seen that. You use the same PrivateProfileString property as with an ini, so that's easy. I have found the API functions are equally easy to use but chose not to use them because they add code that's not unnecessary.

    I, too, find .ini files pretty easy, fast, etc. But the reason I want to find a "better" way is, well, a bit complicated.

    See, my app can be "entered" by the user in various ways. They don't have to start the UI to get to certain features. If they are starting a new document, then, yes, they start the UI, but if they are inserting legal text they just hit a hot-key or click a toolbar button which starts the app as it always starts, but hides the main UI and takes them to the form they need. No matter how the app is used, certain global variables (like the drive letter for their home folder, the file server name where their home folder resides and several others) must be reinitialized. Since these values do not change once Word is started, I would like to keep them "static" somewhere.

    I'm gonna try Rory's suggestion re: toolbar tags or parameters 'cause the toolbar is always there. That just might do the trick.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  8. #8
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Storing Values for Word Session

    Charlotte,

    One of my concerns is the speed at which the various techniques restore the values. I want to by-pass the re-executing of several setup subs everytime a user uses my app. None of the techniques mentioned -- including yours -- is slow, but if I can just read a property value or tag that should do the trick. Thanks for your input.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  9. #9
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,853
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Storing Values for Word Session

    Can you use the Document Variables available in a document to set and recall the variable values. These persist after the macro stops but will cease to be available to other files if you close the document you added them to (unless you stick em in Normal.dot)

    The MS eg looks like this
    ActiveDocument.Variables.Add Name:="Value1", Value:="1"
    MsgBox ActiveDocument.Variables("Value1") + 3
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  10. #10
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Storing Values for Word Session

    That's not a bad idea either. But I would have to put them in the Normal.dot 'cause the values must persist across an entire work day of documents.

    BTW, I am doing something similar with an autotext entry in the Normal.dot. I need to repeatedly retrieve formatted text during the document creation process, so I create an autotext entry in normal and insert it when needed.

    I could do the same thing with Doc Vars in Normal. Good show, Andrew.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  11. #11
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Storing Values for Word Session

    I have lost my concern about speed for obtaining values from disk-based files such as INI files, ESPECIALLY where a user is involved staring at the screen.

    I figure that with today's 64MB RAM machines with disk buffering the data from an INi (or other small file) is sitting right there in RAM until five o'clock.

    I've done no exhaustive testing on this; it just doesn't seem as big an issue as getting the thing working in the first place so that I've something to show the client.

  12. #12
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Storing Values for Word Session

    Yeah, I guess that's accurate. But why make calls to set up variables everytime someone clicks a toolbar button. I don't know, it just doesn't seem right if you can preseve those values is "at the ready" variables or whatever. Maybe it's just a metal block...
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  13. #13
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Storing Values for Word Session

    >make calls to set up variables everytime someone clicks a toolbar button

    As a programmer, I make the calls ("set up the calls") only once, when I write the program code. In real terms, that means I type "Call strGp( .....)" and there's an end to my time commitment. The application is complete and ready to run.

    As a user, someone clicks a toolbar button and then what? If the value is to be preserved between system reboots, then the value has to be stored on disk, right? And that means it has to be read back from disk in order to be used.

    Suppose it is in the document, and that was a good suggestion; then the document is that much bigger but it doesn't make a lot of difference, and once the data is read the data is in a buffer in RAM somewhere.

    Supoose it is in an INI file, then there is one extra disk access (unless you have a multi-KB INI file) and once the data is read the data is in a buffer in RAM somewhere. That is, the entire INI data is in a buffer in RAM somewhere. So after that one access, access time drops to zero.

    So goes my theory.

    Bottom Line, the user probably isn't going to notice any performnace degradation.


    Commercial break: I don't say this often enough, so this is addressed to more than just Kevin's response:

    "I really like the snippets of thinking that you guys get me to do. despite my seeming constant argument, I'm tickled pink to be able to sort out some of these ideas in my head."

Posting Permissions

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