INI Files

To create, read and write to INI files you need to use API calls.

  1. Copy the code, below, into a module
  2. Run the Sub "aIniWriteTest" : if it does not exist, this will create the INI file and write a value to it.
  3. Run the Sub "aIniReadTest" : this will read the INI file and report back the value written to it in (2)

Q: Surely INI files are old technology. Why use INI files in preference to the registry?
A: INI files are more easily transportable to other systems and, (IMHO) far easier to debug at a distance.

Option Explicit
Private 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
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias _
    "WritePrivateProfileStringA" ( _
        ByVal lpApplicationName As String, _
        ByVal lpKeyName As Any, _
        ByVal lpString As Any, _
        ByVal lpFileName As String) As Long
Private Const c_ini_file_name = "Test.Ini"
Private c_WinDir As String
Private c_profile_string As String
Private c_ini_string As String
Private c_ini_no_of_chars As Integer
Sub aIniWriteTest()
    WriteIniFileString "Test", "SubTest", 123
End Sub
Sub aIniReadTest()
    MsgBox ReadIniFileString("Test", "SubTest")
End Sub
'' ***************************************************************************
'' Purpose  : Read from an INI file
''
Public Function ReadIniFileString( _
        ByVal Sect As String, _
        ByVal Keyname As String) As String
Dim worked As Long
Dim RetStr As String * 128
Dim StrSize As Long
    
    c_ini_no_of_chars = 0
    c_ini_string = ""
    
    If Sect = "" Or Keyname = "" Then
        MsgBox "Section Or Key To Read Not Specified !!!", vbExclamation, "INI"
    Else
        c_profile_string = ""
        RetStr = Space(128)
        StrSize = Len(RetStr)
        
        worked = GetPrivateProfileString(Sect, Keyname, "", RetStr, StrSize, _
            ThisWorkbook.Path & "\" & c_ini_file_name)
        If worked Then
            c_ini_no_of_chars = worked
            c_ini_string = Left$(RetStr, worked)
        End If
    End If
    ReadIniFileString = c_ini_string
End Function
'' ***************************************************************************
'' Purpose  : Write to an INI file
''
Public Function WriteIniFileString( _
        ByVal Sect As String, _
        ByVal Keyname As String, _
        ByVal Wstr As String) As String
Dim worked As Long
        
    c_ini_no_of_chars = 0
    c_ini_string = ""
    
    If Sect = "" Or Keyname = "" Then
        MsgBox "Section Or Key To Write Not Specified !!!", vbExclamation, "INI"
    Else
        worked = WritePrivateProfileString(Sect, Keyname, Wstr, _
            ThisWorkbook.Path & "\" & c_ini_file_name)
        If worked Then
            c_ini_no_of_chars = worked
            c_ini_string = Wstr
        End If
        WriteIniFileString = c_ini_string
    End If
    
End Function

Published: 06-Feb-2004
Last edited: 01-Mar-2011 20:51