Saturday, November 08, 2008

How to Read and Write Configuration Files using VBA

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

6 comments:

  1. Anonymous2:08 PM

    I coincidentally wrote these same routines just last week for a VBA project I'm working on. I so much prefer INI files to registry entries for those types of things that the user should be able to modify.

    One feature I tend to add to the function you are calling "GetSectionEntry" is an optional default value. If the value doesn't already exist in the INI file, I return the stated default. I also write the default to the file. This way, defaults are in place (so I don't have to build the INI file in order for the program to work) and the structure gets built as I test, so that when I want to tweak parameters, the default values are already in place.

    This safeguards against typos and other mistakes. Doesn't guarantee against it, but it does help.

    ReplyDelete
  2. Perfect, this code do what I exactly want, with small changes

    ReplyDelete
  3. Thanks, it worked :)

    ReplyDelete
  4. Anonymous12:48 PM

    Nice code works great for what I need. Can use in combination with my code to relocate files and give names based on a single ini file thank you.

    ReplyDelete
  5. Knowing this is an old post; but I am wondering, in your function GetSectionEntry, why you are storing the passed-thru variables into local ones. In my opinion there is absolutely no need for that, for you have declared the pass-thru variables as ByVal so they won't be passed back to the parent.

    ReplyDelete
  6. Yikes - now this does not work in the 64 bit version of PowerPoint. Is there something I am missing?

    ReplyDelete

StumbleUpon
Share on Facebook
Related Posts Plugin for WordPress, Blogger...
Download Windows Live Toolbar and personalize your Web experience! Add custom buttons to get the information you care about most.