Hello WHWALDREP,
I have written 2 macro modules. One contains the API calls for the Provate Profile functions and the second contains the "ReadINI" and "WriteINI" commands that are attached to the buttons. The code for this macros is below. For those who are interested about the Private Profile functions, download the the workbook and read the notes I have included in the module "Read_Write_INI_Files".
You should know that init files typically don't use quotes as there are only 2 data types: Strings and Integers. The test file I created based on your example did not include the quotes. The empty line between sections was also removed as this doesn't conform to the API standard. It will read the information correctly, but the API WriteProfile functions don't include blanks between sections. Here is what the file looks like:
[bInitialization File Layout[/b]
[1243052037]
group = master
names = Wesley,John,Bill
protected = 1
[776843634]
group = member
names = Test,Adam,Joe
protected = 0
[751211588]
group = user
names = Karen,Dell,HP
protected = 0
Macro Code to Read aand Write the Init File
Public FileName As String
Sub ReadINI()
Dim Buffer1 As String
Dim Buffer2 As String
Dim BuffSize As Long
Dim ColOrder As Variant
Dim FileFilters As String
Dim I As Integer
Dim Key As String
Dim Keys As Variant
Dim L1 As Long
Dim ListObj As ListObject
Dim RetVal As Variant
Dim Rng As Range
Dim SectionName As String
If FileName = "" Then
'Select and Open the file.
FileFilters = "Initialization Files (*.ini),*.ini,Text Files (*.txt), *.txt, All Files (*.*), *.*"
RetVal = Application.GetOpenFilename(FileFilters, 1, "Select File")
If RetVal = "False" Then
Exit Sub
Else
FileName = RetVal
End If
End If
Keys = Array("group", "protected", "names")
ColOrder = Array(2, 3, 4)
Set ListObj = ActiveSheet.ListObjects("Table1")
Set Rng = ListObj.Range
'Ignore the header and total rows.
Set Rng = Rng.Columns(1).Offset(1, 0).Resize(RowSize:=Rng.Rows.Count - 2)
R = 1
C = 1
'Create two 8k buffers to hold the Section Names and Keys/Values.
BuffSize = 8192
Buffer1 = Space(BuffSize)
Buffer2 = Space(BuffSize)
'Read in the Section Names.
RetVal = GetPrivateProfileSectionNames(Buffer1, BuffSize, FileName)
'Copy the Section Names to the ListObject and save in an array.
Do
L1 = StrLen(Buffer1)
If L1 = 0 Then Exit Do
SectionName = Left(Buffer1, L1)
Rng.Cells(R, C) = SectionName
For I = 0 To UBound(Keys)
Key = Keys(I)
RetVal = GetPrivateProfileString(SectionName, ByVal Key, "?", Buffer2, BuffSize, FileName)
C = C + 1
Rng.Cells(R, C) = Left(Buffer2, RetVal)
Buffer2 = Space(BuffSize)
Next I
C = 1
R = R + 1
Buffer1 = Right(Buffer1, Len(Buffer1) - L1 - 1)
Loop
End Sub
Sub WriteINI()
Dim ColOrder As Variant
Dim FileFilters As String
Dim Key As String
Dim Keys As Variant
Dim ListObj As ListObject
Dim ProfileString As String
Dim RetVal As Variant
Dim Rng As Range
If FileName = "" Then
MsgBox "There is No File Selected to Write to." & vbCrLf _
& "Please Select a File."
' Select and Open the file.
FileFilters = "Initialization Files (*.ini),*.ini,Text Files (*.txt), *.txt, All Files (*.*), *.*"
RetVal = Application.GetOpenFilename(FileFilters, 1, "Select File")
If RetVal = "False" Then
Exit Sub
Else
FileName = RetVal
End If
End If
Keys = Array("group", "names", "protected")
ColOrder = Array(2, 4, 3)
Set ListObj = ActiveSheet.ListObjects("Table1")
Set Rng = ListObj.Range
'Ignore the header and total rows.
Set Rng = Rng.Offset(1, 0).Resize(RowSize:=Rng.Rows.Count - 2)
'Overwrite the file by deleting it and recreating it.
Kill FileName
N = FreeFile
Open FileName For Output As #N
Close #N
For R = 1 To Rng.Rows.Count
SectionName = Trim(Rng.Cells(R, 1))
If SectionName <> "" Then
ProfileString = ""
For C = 0 To UBound(ColOrder)
Key = Keys(C) & " = "
ProfileString = ProfileString & Key & Rng.Cells(R, ColOrder(C)) & Chr$(0)
Next C
ProfileString = ProfileString & Chr$(0)
RetVal = WritePrivateProfileSection(SectionName, ProfileString, FileName)
End If
Next R
End Sub
Bookmarks