|专用配置文件字符串通常用于在应用程序/文档外部存储用户特定的信息,以供以后使用。

例如,您可以在对话框/用户窗体中存储有关最新内容的信息,打开工作簿的次数或发票模板的上次使用发票编号。

每个用户的专用配置文件字符串可以存储在注册表中。您也可以在本地硬盘或共享网络文件夹上使用INI文件。

这是用于在注册表中写入和读取私有配置文件字符串的示例宏。

' the examples below assumes that the range B3:B5 in the active sheet contains

' information about Lastname, Firstname and Birthdate

Sub WriteUserInfoToRegistry()

' saves information in the Registry to

' HKEY_CURRENT_USER\Software\VB and VBA Program Settings\TESTAPPLICATION

On Error Resume Next

SaveSetting "TESTAPPLICATION", "Personal", "Lastname", Range("B3").Value

SaveSetting "TESTAPPLICATION", "Personal", "Firstname", Range("B4").Value

SaveSetting "TESTAPPLICATION", "Personal", "Birthdate", Range("B5").Value

On Error GoTo 0

End Sub

Sub ReadUserInfoFromRegistry()

' reads information in the Registry from

' HKEY_CURRENT_USER\Software\VB and VBA Program Settings\TESTAPPLICATION

Range("B3").Formula = GetSetting("TESTAPPLICATION", "Personal", "Lastname", "")

Range("B4").Formula = GetSetting("TESTAPPLICATION", "Personal", "Firstname", "")

Range("B5").Formula = GetSetting("TESTAPPLICATION", "Personal", "Birthdate", "")

End Sub

' the example below assumes that the range D4 in the active sheet contains

' information about the unique number

Sub GetNewUniqueNumberFromRegistry()

Dim UniqueNumber As Long

UniqueNumber = 0

On Error Resume Next

UniqueNumber = CLng(GetSetting("TESTAPPLICATION", "Personal", "UniqueNumber", ""))

On Error GoTo 0

Range("D4").Formula = UniqueNumber + 1

SaveSetting "TESTAPPLICATION", "Personal", "UniqueNumber", Range("D4").Value

End Sub

Sub DeleteUserInfoFromRegistry()

' deletes information in the Registry from

' HKEY_CURRENT_USER\Software\VB and VBA Program Settings\TESTAPPLICATION

On Error Resume Next

DeleteSetting "TESTAPPLICATION" ' delete all information

'DeleteSetting "TESTAPPLICATION", "Personal" ' delete one section

'DeleteSetting "TESTAPPLICATION", "Personal", "Birthdate" ' delete one key

On Error GoTo 0

End Sub