WD97: Sample VBA Code to Set, Retrieve .ini File or Registry InformationID: Q160132
|
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.
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
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 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."
Q120802 Office: How to Add/Remove a Single Office Program or Component
Q81792 Using Windows Calls to Write to a Private Text File
Additional query words: wordcon word8 word97 8.0 vb vba vbe
Keywords : kbdta kbwordvba
Version : WINDOWS:97
Platform : WINDOWS
Issue type : kbinfo
Last Reviewed: July 30, 1999