Working with an ini file

doulostheou

Registered User.
Local time
Today, 04:50
Joined
Feb 8, 2002
Messages
314
I found some code online to help work with an ini file. It works and I am able to use it by specifying the path, the section, and the name. There were two functions, one to read the information from the ini file and the other to change the information in the ini file. It uses a word object to accomplish this. My problem is that since I have employed these two functioins I have been running into memory problems. Last time I got an error message saying Word could not open the PDF macro. I am thinking that if I could read and write to the ini file without using Word it may help. I'm just not sure how to go about this. Below is the code I am currently using.

Code:
Function SetIniSetting(FileName As String, Section As String, Key As String, KeyValue) As Boolean
Dim wd As Word.Application
    SetIniSetting = False
    Set wd = New Word.Application ' create the Word application object
    On Error Resume Next
    wd.System.PrivateProfileString(FileName, Section, Key) = CStr(KeyValue)
    On Error GoTo 0
    Set wd = Nothing ' destroy the Word application object
    SetIniSetting = True
End Function

Function GetIniSetting(FileName As String, Section As String, Key As String) As String
Dim wd As Word.Application
    GetIniSetting = ""
    Set wd = New Word.Application ' create the Word application object
    On Error Resume Next
    GetIniSetting = wd.System.PrivateProfileString(FileName, Section, Key)
    On Error GoTo 0
    Set wd = Nothing ' destroy the Word application object
End Function
 
I I went to processes and found that for every time I call either of the functions a WinWord.exe process keeps running. Apparently Set wd = nothing does not destroy the word application object as promised. I preceded this line by wd.Quit and it now appears to work fine. I still would rather read and write to the ini from Access and would appreciate any insight that could be given in this area.
 
Personally I like this method (which does not use any Word Processor but API's)

=============================================
Public Declare Function GetPrivateProfileString _
Lib "kernel32" Alias "GetPrivateProfileStringA" _
(ByVal lpSectionName As String, _
ByVal lpKeyName As Any, _
ByVal lpDefault As String, _
ByVal lpReturnedString As String, _
ByVal nSize As Long, _
ByVal lpFileName As String) As Long

Public Declare Function WritePrivateProfileString _
Lib "kernel32" Alias "WritePrivateProfileStringA" _
(ByVal lpSectionName As String, _
ByVal lpKeyName As Any, _
ByVal lpString As Any, _
ByVal lpFileName As String) As Long


Public Sub ProfileSaveItem(lpSectionName As String, _
lpKeyName As String, _
lpValue As String, _
inifile As String)

'This function saves the passed value to the file,
'under the section and key names specified.
'If the ini file does not exist, it is created.
'If the section does not exist, it is created.
'If the key name does not exist, it is created.
'If the key name exists, it's value is replaced.

Call WritePrivateProfileString(lpSectionName, _
lpKeyName, _
lpValue, _
inifile)

End Sub


Public Function ProfileGetItem(lpSectionName As String, _
lpKeyName As String, _
defaultValue As String, _
inifile As String) As String

'Retrieves a value from an ini file corresponding
'to the section and key name passed.

Dim success As Long
Dim nSize As Long
Dim ret As String

'call the API with the parameters passed.
'The return value is the length of the string
'in ret, including the terminating null. If a
'default value was passed, and the section or
'key name are not in the file, that value is
'returned. If no default value was passed (""),
'then success will = 0 if not found.

'Pad a string large enough to hold the data.
ret = Space$(2048)
nSize = Len(ret)
success = GetPrivateProfileString(lpSectionName, _
lpKeyName, _
defaultValue, _
ret, _
nSize, _
inifile)

If success Then
ProfileGetItem = left$(ret, success)
End If

End Function


Public Sub ProfileDeleteItem(lpSectionName As String, _
lpKeyName As String, _
inifile As String)

'this call will remove the keyname and its
'corresponding value from the section specified
'in lpSectionName. This is accomplished by passing
'vbNullString as the lpValue parameter. For example,
'assuming that an ini file had:
' [Colours]
' Colour1=Red
' Colour2=Blue
' Colour3=Green
'
'and this sub was called passing "Colour2"
'as lpKeyName, the resulting ini file
'would contain:
' [Colours]
' Colour1=Red
' Colour3=Green

Call WritePrivateProfileString(lpSectionName, _
lpKeyName, _
vbNullString, _
inifile)

End Sub


Public Sub ProfileDeleteSection(lpSectionName As String, _
inifile As String)

'this call will remove the entire section
'corresponding to lpSectionName. This is
'accomplished by passing vbNullString
'as both the lpKeyName and lpValue parameters.
'For example, assuming that an ini file had:
' [Colours]
' Colour1=Red
' Colour2=Blue
' Colour3=Green
'
'and this sub was called passing "Colours"
'as lpSectionName, the resulting Colours
'section in the ini file would be deleted.

Call WritePrivateProfileString(lpSectionName, _
vbNullString, _
vbNullString, _
inifile)

End Sub


Public Function StripNulls(startStrg As String) As String

'take a string separated by nulls,
'split off 1 item, and shorten the string
'so the next item is ready for removal.

'The passed string must have a terminating
'null for this function to work correctly.
'If you remain in a loop, check this first!

Dim pos As Long
Dim item As String

pos = InStr(1, startStrg, Chr$(0))

If pos Then

item = Mid$(startStrg, 1, pos - 1)
startStrg = Mid$(startStrg, pos + 1, Len(startStrg))
StripNulls = item

End If

End Function


Public Function ProfileLoadList(lpSectionName As String, _
inifile As String) As String

'This is the main function for loading the listbox data
'from an ini file. First, it calls GetPrivateProfileString
'to get all key name entries under lpSectionName. It then loops,
'passing each key name to ppGetItemsInfo(), and the returned
'value is added to the listbox.
Dim lst As String
Dim success As Long
Dim c As Long
Dim nSize As Long
Dim KeyData As String
Dim lpKeyName As String
Dim ret As String

'call the API passing null as the parameter
'for the lpKeyName parameter. This causes
'the API to return a list of all keys under
'that section. Pad the passed string large
'enough to hold the data.
ret = Space$(2048)
nSize = Len(ret)
success = GetPrivateProfileString(lpSectionName, _
vbNullString, _
"", _
ret, _
nSize, _
inifile)

'The returned string is a null-separated
'list of key names, terminated by a pair
'of null characters. If the Get call was
'successful, success holds the length of the
'string in ret up to but not including
'that second terminating null. The
'ProfileGetItem function below extracts
'each key item using the nulls as markers,
'so trim off the terminating null.
If success Then

'trim terminating null and trailing spaces
ret = left$(ret, success)

'with the resulting string,
'extract each element
Do Until ret = ""

'strip off an item (i.e. "Item1", "Item2")
lpKeyName = StripNulls(ret)

'pass the lpKeyName received to a routine that
'again calls GetPrivateProfileString, this
'time passing the real key name. Returned
'is the value associated with that key,
'ie the "Apple" corresponding to the ini
'entry "Item1=Apple"
KeyData = ProfileGetItem(lpSectionName, _
lpKeyName, _
"", _
inifile)

'add the item retruned to the listbox
lst = lst & lpKeyName & "=" & KeyData & "^"

Loop

End If

'return the number of items as an
'indicator of success
ProfileLoadList = lst

End Function


Public Sub MakeLocalINIFile(ByVal strFileName As String, ParamArray DefaultItems() As Variant)
On Error GoTo ErrorHandler
Dim intFileNum As Integer
Dim strNew As String
Dim intloop As Integer

intFileNum = FreeFile

strNew = "[" & App.Title & "]" & Chr(13) & Chr(10)

strNew = strNew & Join(DefaultItems, Chr(13) & Chr(10))

Open strFileName For Output As intFileNum
Print #intFileNum, strNew
Close #intFileNum

Exit Sub
ErrorHandler:
Err.Clear

End Sub
 
Thank you. Your code speeded up my application a great deal.
 

Users who are viewing this thread

Back
Top Bottom