Microsoft Excel에서 VBA를 사용하여 INI-파일을 사용하여 개인 프로필 문자열
개인 프로필 문자열은 나중에 사용하기 위해 응용 프로그램 / 문서 외부에 사용자 특정 정보를 저장하는 데 자주 사용됩니다.
예를 들어 대화 상자 / 사용자 양식의 최신 콘텐츠에 대한 정보, 통합 문서를 연 횟수 또는 송장 템플릿에 대해 마지막으로 사용한 송장 번호를 저장할 수 있습니다.
정보는 로컬 하드 디스크 또는 공유 네트워크 폴더에있는 INI 파일에 저장할 수 있습니다.
INI 파일은 일반 텍스트 파일이며 내용은 다음과 같을 수 있습니다.
Lastname = Doe Firstname = John Birthdate = 1.1.1960 UniqueNumber = 123456 각 사용자의 개인 프로필 문자열도 레지스트리에 저장할 수 있습니다.
Excel에는 Word (System.PrivateProfileString)와 같은 INI 파일 읽기 및 쓰기를위한 기본 제공 기능이 없으므로이 작업을 쉽게 수행하려면 몇 가지 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