This function WriteReadIni
can be used easily for reading and writing from an INI file :
Here is the English Version :
Option Explicit
Dim Title,fso,oFile,PlayerName,NewPlayerName
Title = "Read and Write from INI file"
Set fso = CreateObject("Scripting.FileSystemObject")
Set oFile = fso.GetFile("Settings.ini")
PlayerName = WriteReadIni(oFile,"Settings","PlayerName",Null) 'For reading only
MsgBox "The content of this variable PlayerName read from Settings.ini file is : " & vbCrlf &_
"PlayerName = " & PlayerName, vbInformation,Title
Do
PlayerName = inputbox("Please enter your name")
Loop Until PlayerName <> ""
NewPlayerName = WriteReadIni(oFile,"Settings","PlayerName",PlayerName) ' Writing the new variable to ini file
NewPlayerName = WriteReadIni(oFile,"Settings","PlayerName",Null) ' Reading the new variable
MsgBox "The New content of this variable PlayerName read from Settings.ini file is : " & vbCrlf &_
"PlayerName = " & NewPlayerName, vbInformation,Title
'-----------------------------------------------------------------------------------------------------------------------
Function WriteReadIni(oFile,section,key,value)
'-----------------------------------------------------------------------------------------------------------------------
' omen999 - mars 2018 v 1.1 - http://omen999.developpez.com/
' writes / reads the <key> of the <section> section of the oFile file object with the value <value> if read: value = Null
' In writing if the section and / or the key do not exist, they will be created
' In writing returns false if the key / value pair already existed if not true
' In read returns either: the key value, an empty string in the case of an empty key or False if the key does not exist
'-----------------------------------------------------------------------------------------------------------------------
Dim oText,iniText,sectText,newSectText,keyText,reg,regSub
' Initialization of regex objects
' Can be moved in the main code in case of successive calls
Set reg = New RegExp
Set regSub = New RegExp
reg.MultiLine=True 'simplifie le pattern
reg.IgnoreCase = True
regSub.IgnoreCase = True
Set oText = oFile.OpenAsTextStream(1,0)
iniText = oText.ReadAll
oText.Close
reg.Pattern = "^\[" & section & "\]((.|\n[^\[])+)"
regSub.Pattern = "\b" & key & " *= *([^;\f\n\r\t\v]*)"
On Error Resume Next
If IsNull(value) Then ' key reading
WriteReadIni = regSub.Execute(reg.Execute(iniText).Item(0).SubMatches(0)).Item(0).SubMatches(0)
If Err.Number = 5 then WriteReadIni = False
Else ' key writing
sectText = reg.Execute(iniText).Item(0).SubMatches(0)
If Err.Number = 5 Then ' Unknown section
iniText = iniText & vbCrLf & "[" & section & "]" & vbCrLf & key & "=" & value
Else
newSectText = regSub.Replace(sectText,key & "=" & value)
If newSectText = sectText Then ' No replacement noted. either the key / value already exists or it is a new key
If regSub.Test(sectText) Then ' The key / value pair already exists -> Exit Function
WriteReadIni = False
Exit Function
End If
If Right(sectText,1) = vbCr Then keyText = key & "=" & value Else keyText = vbCrLf & key & "=" & value
newSectText = sectText & keyText
End If
iniText = reg.Replace(iniText,"[" & section & "]" & newSectText)
End If
Set oText = oFile.OpenAsTextStream(2,0)
oText.Write iniText
oText.Close
WriteReadIni = True
End If
End Function
'-----------------------------------------------------------------------------------------------------------------------