使用Microsoft Excel中使用VBA INI档案私人档案字符串
专用配置文件字符串通常用于在应用程序/文档外部存储用户特定的信息,以供以后使用。
例如,您可以在对话框/用户窗体中存储有关最新内容的信息,打开工作簿的次数或发票模板上次使用的发票编号的信息。
信息可以存储在本地硬盘或共享网络文件夹中的INI文件中。
INI文件是一个普通的文本文件,其内容可能类似于以下内容:
Lastname = Doe Firstname = John Birthdate = 1.1.1960 UniqueNumber = 123456每个用户的专用配置文件字符串也可以存储在注册表中。
Excel没有内置的功能来读写INI文件,例如Word的(System.PrivateProfileString),因此您需要几个API函数来轻松实现此目的。
这是用于写入和读取包含私有配置文件字符串的INI文件的示例宏。
const IniFileName As String =“ C:\ FolderName \ UserInfo.ini”
‘包含要读取/写入的信息的文件的路径和文件名
Private Declare Function GetPrivateProfileStringA Lib _ "Kernel32" (ByVal strSection As String, _ ByVal strKey As String, ByVal strDefault As String, _ ByVal strReturnedString As String, _ ByVal lngSize As Long, ByVal strFileNameName As String) As Long Private Declare Function WritePrivateProfileStringA Lib _ "Kernel32" (ByVal strSection As String, _ ByVal strKey As String, ByVal strString As String, _ ByVal strFileNameName As String) As Long Private Function WritePrivateProfileString32(ByVal strFileName As String, _ ByVal strSection As String, ByVal strKey As String, _ ByVal strValue As String) As Boolean Dim lngValid As Long On Error Resume Next lngValid = WritePrivateProfileStringA(strSection, strKey, _ strValue, strFileName) If lngValid > 0 Then WritePrivateProfileString32 = True On Error GoTo 0 End Function Private Function GetPrivateProfileString32(ByVal strFileName As String, _ ByVal strSection As String, ByVal strKey As String, _ Optional strDefault) As String Dim strReturnString As String, lngSize As Long, lngValid As Long On Error Resume Next If IsMissing(strDefault) Then strDefault = "" strReturnString = Space(1024) lngSize = Len(strReturnString) lngValid = GetPrivateProfileStringA(strSection, strKey, _ strDefault, strReturnString, lngSize, strFileName) GetPrivateProfileString32 = Left(strReturnString, lngValid) On Error GoTo 0 End Function ' the examples below assumes that the range B3:B5 in the active sheet contains ' information about Lastname, Firstname and Birthdate Sub WriteUserInfo() ' saves information in the file IniFileName If Not WritePrivateProfileString32(IniFileName, "PERSONAL", _ "Lastname", Range("B3").Value) Then MsgBox "Not able to save user info in " & IniFileName, _ vbExclamation, "Folder does not exist!" Exit Sub End If WritePrivateProfileString32 IniFileName, "PERSONAL", _ "Lastname", Range("B3").Value WritePrivateProfileString32 IniFileName, "PERSONAL", _ "Firstname", Range("B4").Value WritePrivateProfileString32 IniFileName, "PERSONAL", _ "Birthdate", Range("B5").Value End Sub Sub ReadUserInfo() ' reads information from the file IniFileName If Dir(IniFileName) = "" Then Exit Sub Range("B3").Formula = GetPrivateProfileString32(IniFileName, _ "PERSONAL", "Lastname") Range("B4").Formula = GetPrivateProfileString32(IniFileName, _ "PERSONAL", "Firstname") Range("B5").Formula = GetPrivateProfileString32(IniFileName, _ "PERSONAL", "Birthdate") End Sub ' the example below assumes that the range D4 in the active sheet contains ' information about the unique number Sub GetNewUniqueNumber() Dim UniqueNumber As Long If Dir(IniFileName) = "" Then Exit Sub UniqueNumber = 0 On Error Resume Next UniqueNumber = CLng(GetPrivateProfileString32(IniFileName, _ "PERSONAL", "UniqueNumber")) On Error GoTo 0 Range("D4").Formula = UniqueNumber + 1 If Not WritePrivateProfileString32(IniFileName, "PERSONAL", _ "UniqueNumber", Range("D4").Value) Then MsgBox "Not able to save user info in " & IniFileName, _ vbExclamation, "Folder does not exist!" Exit Sub End If End Sub