WD97: Sample VB Code to Set, Retrieve .ini File or Registry Info

Last reviewed: February 11, 1998
Article ID: Q160132
The information in this article applies to:
  • Microsoft Word 97 for Windows

IMPORTANT: This article contains information about editing the registry. Before you edit the registry, you should first make a backup copy of the registry files (System.dat and User.dat). Both are hidden files in the Windows folder.

SUMMARY

In a Microsoft Visual Basic for Applications macro you can use the PrivateProfileString property to store and retrieve settings from an ini file or the Windows registry.

MORE INFORMATION

Microsoft provides examples of Visual Basic for Applications procedures for illustration only, without warranty either expressed or implied, including, but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft support engineers can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400.

WARNING: Using Registry Editor incorrectly can cause serious problems that may require you to reinstall Windows 95 or Windows NT. Microsoft cannot guarantee that problems resulting from the incorrect use of Registry Editor can be solved. Use Registry Editor at your own risk.

For information about how to edit the registry, view the Changing Keys And Values online Help topic in Registry Editor (Regedit.exe). Note that you should make a backup copy of the registry files (System.dat and User.dat) before you edit the registry.

The following macro examples use the PrivateProfileString property to set or return a string from a settings file or the Windows registry.

NOTE: If the file, section, or key do not exist, they are created when you run the macro.

The following macro creates a file ("Test.ini"), section ("MySection"), and key ("MyKey") and sets the value of the key to "7."

   Sub SetInfo()
      System.PrivateProfileString("Test.ini", "MySection", "MyKey") = "7"
   End Sub

The following macro returns the value set in the previous macro.

   Sub GetInfo()
      MsgBox System.PrivateProfileString("Test.ini", "MySection", "MyKey")
   End Sub

You cannot delete a key using the PrivateProfileString property, but you can set it's argument to an empty string. The following macro deletes the value, but it does not delete the MySection section or the MyKey entry from the Test.ini file.

   Sub DelInfo()
      System.PrivateProfileString("Test.ini", "MySection", "MyKey") = ""
   End Sub

The following macro displays the default user name setting from the Windows registry:

   Sub GetUserInfo()
      aName = System.PrivateProfileString("", _
      "HKEY_CURRENT_USER\Software\Microsoft\MS Setup (ACME)\User Info", _
      "DefName")
      MsgBox aName
   End Sub

For more information about PrivateProfileString, while in the Visual Basic for Applications Editor, click the Office Assistant, type "PrivateProfileString" (without the quotation marks), click Search, and then click to view "PrivateProfileString Property."

NOTE: If the Assistant is hidden, click the Office Assistant button on the Standard toolbar. If Microsoft Visual Basic Help is not installed on your computer, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q120802
   TITLE     : Office: How to Add/Remove a Single Office
               Program or Component

For information about how to do this in earlier versions of Word, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q81792
   TITLE     : Using Windows Calls to Write to a Private Text File


Additional query words: word8 word97 8.0 8.0 vb vba vbe
Keywords : kbwordvba kbfaq kbmacro kbusage
Version : 97
Platform : WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 11, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.