Attribute VB_Name = "Settings" '*********************************************************************** '*********************************************************************** 'This code module contains code that has to do with the INI settings. 'Including: ' Set up default settings ' Get settings ' Set settings ' -Also, a function that returns th local printer settings (private) '*********************************************************************** '*********************************************************************** Declare Function GetPrivateProfileSection Lib "kernel32" _ Alias "GetPrivateProfileSectionA" _ (ByVal lpApplicationName As String, ByVal lpReturnedString As String, _ ByVal nSize As Long, ByVal lpFileName As String) As Long Declare Function GetPrivateProfileString Lib "kernel32" _ Alias "GetPrivateProfileStringA" _ (ByVal lpApplicationName As String, ByVal lpKeyName As String, _ 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 lpApplicationName As String, ByVal lpKeyName As String, _ ByVal lpString As Any, ByVal lpFileName As String) As Long Option Explicit '*********************************************************************** ' Comments : Returns names of all keys in gien section from INI file ' Parameters : SectionName - string representing the section to search ' KeyName - string representing the key containing the value to return ' FileName - Optional string representing the INI filename & path ' If not provided, the app.path and app.title is assumed ' strDelimiterChar ==> optional char to be used as ' delimiter between settings ' Returns : A delimited list of all ini setings and values ' Created : Enterprise Software Solutions [05/26/1999] '*********************************************************************** Public Function GetAllINISettingsInSection(SectionName As String, _ Optional FileName As Variant, _ Optional strDelimiterChar As String = "|") As String Const lngSize As Long = 10000 Dim strBuffer As String * 10000 Dim lngReturnCode As Long Dim strINIString As String On Error GoTo ErrorHandler 'fill in file name parm (if not provided) If IsMissing(FileName) Then 'If app.path is in root, it will have a "\" already 'Otherwise add a "\" between app.path and the file name If Right(App.Path, 1) = "\" Then FileName = App.Path & App.Title & ".INI" Else FileName = App.Path & "\" & App.Title & ".INI" End If End If 'call the API function lngReturnCode = GetPrivateProfileSection(SectionName, _ strBuffer, lngSize, FileName) If lngReturnCode <> 0 Then strINIString = Left(strBuffer, lngReturnCode) Else strINIString = "" End If 'put our delimiter in the string strINIString = Replace(strINIString, Chr(0), _ strDelimiterChar, , , vbBinaryCompare) 'set return value GetAllINISettingsInSection = strINIString Exit Function ErrorHandler: GetAllINISettingsInSection = "" RaiseSameError "GetAllINISettingsInSection()" End Function '*********************************************************************** ' Comments : Returns a setting from an INI file ' Parameters : SectionName - string representing the section to search ' KeyName - string representing the key containing the value to return ' FileName - Optional string representing the INI filename & path ' If not provided, the app.path and app.title is assumed ' DefaultValue - string representing the value to return if the key is not found ' If not provided, an empty string is assumed ' Returns : The value of the INI setting as string ' Created : Enterprise Software Solutions [05/26/1999] '*********************************************************************** Public Function GetINISetting(SectionName As String, _ KeyName As String, Optional FileName As Variant, _ Optional DefaultValue As Variant) As String Const lngSize As Long = 1000 Dim strBuffer As String * 1000 Dim lngReturnCode As Long On Error GoTo ErrorHandler If IsMissing(DefaultValue) Then DefaultValue = "" End If 'get filename (if not passed in) If IsMissing(FileName) Then 'If app.path is in root, it will have a "\" already 'Otherwise add a "\" between app.path and the file name If Right(App.Path, 1) = "\" Then FileName = App.Path & App.Title & ".INI" Else FileName = App.Path & "\" & App.Title & ".INI" End If End If lngReturnCode = GetPrivateProfileString(SectionName, KeyName, _ DefaultValue, strBuffer, lngSize, FileName) If lngReturnCode <> 0 Then GetINISetting = Left(strBuffer, lngReturnCode) Else GetINISetting = "" End If Exit Function ErrorHandler: 'error handler GetINISetting = "" RaiseSameError End Function '*********************************************************************** ' Comments : Sets a setting from an INI file ' Parameters : SectionName - string representing the section to search ' KeyName - string representing the key containing the ' value to return ' FileName - Optional string representing the INI filename ' & path ' If not provided, the app.path and app.title ' is assumed ' DefaultValue - string representing the value to return ' if the key is not found ' If not provided, an empty string is assumed ' Returns : True if successfull ' Created : Enterprise Software Solutions [05/13/1999] '*********************************************************************** Public Sub SetINISetting(SectionName As String, _ KeyName As String, Value As String, _ Optional FileName As Variant) Dim lngReturnCode As Long On Error GoTo ErrorHandler If IsMissing(FileName) Then 'If app.path is in root, it will have a "\" already 'Otherwise add a "\" between app.path and the file name If Right(App.Path, 1) = "\" Then FileName = App.Path & App.Title & ".INI" Else FileName = App.Path & "\" & App.Title & ".INI" End If End If lngReturnCode = WritePrivateProfileString(SectionName, KeyName, _ Value, FileName) If lngReturnCode = 0 Then Err.Raise 5040, "Custom Error", "Could not set INI setting" End If Exit Sub ErrorHandler: RaiseSameError End Sub