Read an INI file from VBA

 

'this function is used to get .ini data
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

 

'get the current directory
strUrl = CurDir
'read the ini file and get the SSRS report path
strUrl = GetReportPath("DD", "RMAReportPath", strUrl & "\DD.ini")

 

 

Function GetReportPath(ByVal strSectionName As String, ByVal strEntry As String, ByVal strIniPath As String) As String
    'this funtion returns data from an ini file
    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
        GetReportPath = sValue
    Else
        GetReportPath = vbNullChar
    End If
      
ErrGetSectionentry:
    If Err <> 0 Then
    Err.Clear
    Resume Next
    End If
  
  
End Function

RealWorldCode gives developers practical, real‑world solutions with clean, working code — no fluff, no theory, just answers.
Links
Home
Knowledge Areas
Sitemap
Contact
Et cetera
Privacy Policy
Terms and Conditions
Cookie Preferences