How to Read and Write INI files using VBA
Ini files were extensively used for storing configuration path. Of late this has been replaced with XML. Here is a small snippet for using
Here is a sample INI File
[MOSS_Documents]
AdminDocPath=d:\MOSS\AdminDocs\
ArchiveDocPath="d:\MOSS\DocsArchive\"
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
Declare Function WritePrivateProfileString Lib "kernel32" _
Alias "WritePrivateProfileStringA" _
(ByVal sSectionName As String, _
ByVal sKeyName As String, _
ByVal sString As String, _
ByVal sFileName As String) As Long
Private Function GetSectionEntry(ByVal strSectionName As String, ByVal strEntry As String, ByVal strIniPath As String) As String
'===================================================================================
'Purpose : Retrieve data from Sample.ini
'Assumptions:
'Effects :
'Inputs : Section Name and Entry Name in the KitBOM ini
'Returns : Value of the Entry in the section
'===================================================================================
Dim X As Long
Dim sSection As String, sEntry As String, sDefault As String
Dim sRetBuf As String, iLenBuf As Integer, sFileName As String
Dim sValue As String
On Error GoTo ErrGetSectionentry
sSection = strSectionName
sEntry = strEntry
sDefault = ""
sRetBuf = Strings.String$(256, 0) '256 null characters
iLenBuf = Len(sRetBuf$)
sFileName = strIniPath
X = GetPrivateProfileString(sSection, sEntry, _
"", sRetBuf, iLenBuf, sFileName)
sValue = Strings.Trim(Strings.Left$(sRetBuf, X))
If sValue <> "" Then
GetSectionEntry = sValue
Else
GetSectionEntry = vbNullChar
End If
ErrGetSectionentry:
If Err <> 0 Then
Err.Clear
Resume Next
End If
End Function
Here is the sub to drive the above function
Sub Store_And_Retrieve_Values()
Dim sIniPath
Dim sBuffer
Dim ret
sIniPath = "d:\temp\VBA_MOSS.ini"
sBuffer = GetSectionEntry("MOSS_Documents", "AdminDocPath", sIniPath)
' Write Data to Ini File
ret = WritePrivateProfileString("MOSS_Documents", "AdminDocPath", "d:\MOSSNew\AdminDocs\", sIniPath)
End Sub
The following will be the output INI file after running the macro
[MOSS_Documents]
AdminDocPath=d:\MOSSNew\AdminDocs\
ArchiveDocPath="d:\MOSS\DocsArchive\"
Keywords: Write INI files using VBA, Read files using VBA Read Configuration Files using VBA, Write Configuration Files using VBA, Excel VBA Ini files, VBA GetPrivateProfileString Function, VBA WritePrivateProfileString function, Read and Write Ini files using Excel VBA, Update an Section in Ini file using VBA, Overwrite a string in INI file using VBA, Replace a string in INI file using VBA
Showing posts with label Read files using VBA Read Configuration Files using VBA. Show all posts
Showing posts with label Read files using VBA Read Configuration Files using VBA. Show all posts
Saturday, November 08, 2008
Subscribe to:
Posts (Atom)
Download Windows Live Toolbar and personalize your Web experience! Add custom buttons to get the information you care about most.