There are a couple of API's specificaly designed for reading and writing ini
files. I wrapped them up in some code to make them a bit easier to use. Here
is a description of how to impliment that code.
Open a standard code module. Name the module modIniSettings. Paste the
following...
'***************
'API Function Declarations
Public Declare Function GetPrivateProfileString Lib "kernel32" Alias
"GetPrivateProfileStringA" ( _
ByVal lpApplicationName As String, _
ByVal lpKeyName As Any, _
ByVal lpDefault As String, _
ByVal lpReturnedString As String, _
ByVal nSize As Long, _
ByVal lpFileName As String) As Long
Declare Function WritePrivateProfileString Lib "kernel32.dll" Alias
"WritePrivateProfileStringA" ( _
ByVal lpApplicationName As String, _
ByVal lpKeyName As Any, _
ByVal lpString As Any, _
ByVal lpFileName As String) As Long
'Global Variables
Public INI_SETTINGS As clsINISettings
Public Sub Auto_Close()
Set INI_SETTINGS = Nothing
End Sub
'***********************************
Now add a Class Module to your project. Name the module clsINISettings.
Paste the following...
'********************************
Private c_strSection As String
Private c_strKey As String
Private c_strININame As String
Public Property Let Section(strSection As String)
c_strSection = strSection
End Property
Public Property Let Key(strKey As String)
c_strKey = strKey
End Property
Public Property Let ININame(strININame As String)
c_strININame = strININame
End Property
Public Function ReadINISettings() As Variant
Dim sDestination As String * 255
Dim lReturnVal As Long
On Error GoTo ErrorHandler
lReturnVal = GetPrivateProfileString(c_strSection, c_strKey, "", _
sDestination, Len(sDestination), c_strININame)
'lReturnVal will always equal the length of the returned string not
including vbNullChar 's at the end!!!
If lReturnVal <> 0 Then
sDestination = Left(sDestination, InStr(sDestination, Chr(0)) - 1)
'chr(0)=vbNullChar
ReadINISettings = Trim(sDestination)
Else
Err.Raise vbObjectError + 513 + 1001,
"clsINISettings.ReadINISettings", _
"Initialization File Error!"
End If
Exit Function
ErrorHandler:
MsgBox "Error Number: " & Err.Number & vbCrLf & _
"Error Description: " & Err.Description _
, vbCritical, "Initialization File Error"
End Function
Public Sub WriteINISettings(ByRef strWriteValue As String)
Dim lReturnVal As Long
On Error GoTo ErrorHandler
lReturnVal = WritePrivateProfileString(c_strSection, c_strKey,
strWriteValue, _
c_strININame)
If lReturnVal = 0 Then Err.Raise vbObjectError + 513 + 1001, _
"clsINISettings.WriteINISettings", "Initialization File Error!"
Exit Sub
ErrorHandler:
MsgBox "Error Number: " & Err.Number & vbCrLf & _
"Error Description: " & Err.Description _
, vbCritical, "Initialization File Error"
End Sub
'****************************************
Now to write an INI file is as easy as (paste this code in any standard
module or sheet)...
Sub test()
Set INI_SETTINGS = New clsINISettings
With INI_SETTINGS
.ININame = ThisWorkbook.Path & "\MyIni.ini"
.Section = "StartUp"
.Key = "UID"
.WriteINISettings ("Me")
.Key = "Server"
.WriteINISettings ("MyServer")
.Key = "DBName"
.WriteINISettings ("MyDB")
.Key = "Driver"
.WriteINISettings ("MyDriver")
.Section = "Test"
.Key = "Test"
.WriteINISettings ("That")
End With
Set INI_SETTINGS = Nothing
End Sub