Kenny Bones
Kenny Bones

Reputation: 5129

Read entire ini section and put into array

Ok, so I have these functions I'm tring to use via my vba code. It's probably the as it would have been with vbs as well.

Here's the function(s)

'declarations for working with Ini files
Private Declare Function GetPrivateProfileSection Lib "kernel32" Alias _
"GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, _
ByVal nSize As Long, ByVal lpFileName As String) As Long

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

'// INI CONTROLLING PROCEDURES
'reads an Ini string
Public Function ReadIni(Filename As String, Section As String, Key As String) As String
Dim RetVal As String * 255, v As Long
v = GetPrivateProfileString(Section, Key, "", RetVal, 255, Filename)
ReadIni = Left(RetVal, v + 0)
End Function

'reads an Ini section
Public Function ReadIniSection(Filename As String, Section As String) As String
Dim RetVal As String * 255, v As Long
v = GetPrivateProfileSection(Section, RetVal, 255, Filename)
ReadIniSection = Left(RetVal, v + 0)
End Function

How can I use this to create a function that basically allows me to specify only the section I want to look in, and then find each ini string within that section and put it into an array and return that Array so I can do a loop with it?

Edit: I see that ReadIniSection returns all of the keys in a huge string. Meaning, I need to split it up.

ReadIniSection returns something that looks like this: "Fornavn=FORNAVN[]Etternavn=ETTERNAVN" etc etc. The[] in the middle there isn't brackets, it's a square. Probably some character it doesn't recognize. So I guess I should run it through a split command that takes the value between a = and the square.

Upvotes: 0

Views: 8731

Answers (1)

tpascale
tpascale

Reputation: 2576

See if this helps - splitting on nullchar \0:

Private Sub ListIniSectionLines()
    Dim S As String: S = ReadIniSection("c:\windows\win.ini", "MAIL")
    Dim vLines As Variant: vLines = Split(S, Chr$(0))
    Dim vLine As Variant
    For Each vLine In vLines
       Debug.Print vLine
    Next vLine
End Sub

Upvotes: 2

Related Questions