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 sSub "aIniReadTest" : this will read the INI file and report back
the value written to it in (2)
'' - - - - - - - Code Starts
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", 99
End Sub
''
***************************************************************************
Sub aIniReadTest()
MsgBox ReadIniFileString("Test", "SubTest")
End Sub
''
***************************************************************************
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
''
***************************************************************************
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
'' - - - - - - - Code Ends
--
Regards
Andy Wiggins
www.BygSoftware.com
Home of "Save and BackUp",
"The Excel Auditor" and "Byg Tools for VBA"