Results 1 to 4 of 4
  1. #1
    2 Star Lounger
    Join Date
    Jun 2002
    Posts
    122
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Word Templates & txt/ini files (Word 97 (SR2))

    Hi there, I have a number of templates where users are required to fill in information such as name, e-mail address, telephone number etc. At the moment this information requires to be completed each and every time a user selects one of the templates. My aim is to create either a .txt or .ini file to be stored on each user's hard drive so that it may be possible for me to write some code into the templates in order that if xxxxx.txt or xxxxx.ini is present within C:xxx the corresponding information in that file is inserted automatically into the appropriate fields in the template.

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

    Re: Word Templates & txt/ini files (Word 97 (SR2))

    The "modern" way is to store those values in the Windows Registry, but using a text file (.ini or .txt) is also possible. You can use the PrivateProfileString property of the System object in both cases.

    To write the name Johnson to the key LastName in the section User Settings in the file C:MyTemplate.ini, use

    System.PrivateProfileString("C:MyTemplate.ini", "User Settings", "LastName") = "Johnson"

    To retrieve this value and assign it to the variable strMyVariable, use:

    strMyVariable = System.PrivateProfileString("C:MyTemplate.ini", "User Settings", "LastName")

    To write the name Johnson to the registry (in the LastName value of the User Settings key under HKEY_CURRENT_USERSoftwareMyTemplate), use:

    System.PrivateProfileString("", "HKEY_CURRENT_USERSoftwareMyTemplateUser Settings", "LastName") = "Johnson"

    To retrieve this value and assign it to the variable strMyVariable, use:

    strMyVariable = System.PrivateProfileString("", "HKEY_CURRENT_USERSoftwareMyTemplateUser Settings", "LastName")

    You'll have to create a form in which the user can enter their information; when the users clicks OK, store the values. Retrieve them in the Document_New event of the ThisDocument object of the template.

  3. #3
    Star Lounger
    Join Date
    Jun 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word Templates & txt/ini files (Word 97 (SR2))

    Violet,

    The Registry is the modern way to save this info, but in my case we use a standardized enviroment and messing with the users registry is a no-no.
    My INI file holds the users data, and I use a freeware class module to simplify loading my userforms (and saving new info back).
    Though the module (details below) is freeware I can't actually remember where I got it.

    ' Class: cIniFile
    ' Author: Steve McMahon
    ' Date : 21 Feb 1997

    As an example the following code Writes the contents of a textbox directly to the INI, the class modules functions are used in the same way as Words native ones.

    With m_cIni
    .Key = "TITLE1"
    .strValue = UCase(Me.txtTitle1.Value)
    End With

    If you decide to use INI files and they don't contain many sections then the standard VBA calls are sufficient. But if your files hold a lot of info (ours can hold up to 300 sections)
    then a freeware class module is the way to go.

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Word Templates & txt/ini files (Word 97 (SR2))

    That class library can be found on http://www.vbaccelerator.com/ on this page.

Posting Permissions

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