WD2000: Sample Visual Basic for Applications Code to Set, Retrieve .ini File or Registry Information

ID: Q237436


The information in this article applies to:
  • Microsoft Word 2000

IMPORTANT: This article contains information about editing the registry. Before you edit the registry, make sure you understand how to restore it if a problem occurs. For information about how to do this, view the "Restoring the Registry" Help topic in Regedit.exe or the "Restoring a Registry Key" Help topic in Regedt32.exe.

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 programming examples 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. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Solution Provider or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Solution Providers, please see the following page on the World Wide Web:

http://www.microsoft.com/mcsp/
For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://www.microsoft.com/support/supportnet/overview/overview.asp
WARNING: Using Registry Editor incorrectly can cause serious problems that may require you to reinstall your operating system. 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" Help topic in Registry Editor (Regedit.exe) or the "Add and Delete Information in the Registry" and "Edit Registry Data" Help topics in Regedt32.exe. Note that you should back up the registry before you edit it. If you are running Windows NT, you should also update your Emergency Repair Disk (ERD).


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 does not exist, it is created when you run the macro.

The following macro creates an initialization 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 its argument to an empty string. The following macro deletes the value but 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, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type "PrivateProfileString Property" in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

Additional query words: vba

Keywords : kbdta kbwordvba wd2000
Version : WINDOWS:2000
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: July 16, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.