Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reading .ini file in Excel

Status
Not open for further replies.

drluggo

Programmer
Jan 14, 2003
39
US
I have an Excel application that is required to read data from an .ini file for configuration settings. There are several settings that I read in my main form and I declared the function as follows:

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

In the code module for this form I access the data using the following subroutine:

Private Sub UserForm_Activate()

'**** Local variables
Dim fs

Dim bFound As Boolean

Dim sSection As String
Dim sKeyName As String
Dim sKeyValue As String
Dim intNullValue As Long

Dim lRtnSts As Long

'**** Set application directory
sAppPath = "c:\"
sAppPath = Application.ThisWorkbook.CustomDocumentProperties("VBAppPath")

'**** Determine existence of config file
sCfgFile = sAppPath & "\AET.cfg"
Set fs = CreateObject("Scripting.FileSystemObject")
bFound = fs.FileExists(sCfgFile)

'**** If the config file exists, set the path to the saved export
'**** directory. Otherwise, just set to c: drive
If (bFound) Then
sSection = "DIR"
sKeyName = "EXPORTDIR"
sKeyValue = Space(255)
lRtnSts = GetPrivateProfileString( _
sSection, _
sKeyName, _
"", _
sKeyValue, _
256, _
sCfgFile)

If lRtnSts > 0 Then

'**** . . Textbox value is the stored export directory
TextBox1.Text = Trim$(sKeyValue)

End If

Else

'**** . Textbox value is the c: drive
TextBox1.Text = "c:\"
End If

CommonDialog1.InitDir = TextBox1.Text
Set fs = Nothing

End Sub


THE ABOVE IS WORKING FINE!!!

I have a need to access the getprivateprofilestring API again after the main form is closed. This is doen from another code module in the workbook. Since I cannot decalre the API funciton as Public, I am forced to repeat the decalaration in the other module where I need to use the function.

Here is the problem:

When I try to read the .ini file from this other module, I getr all blanks in the return string (sKeyValue). The interesting thing is that the lRtnSts value has the correct number of cahracters in the string, but the returned string is all blnaks. For example if my .ini file looks like:

[DIR]
EXPORTDIR="c:\temp"

and I attempt to read from the main form I get:

lRtnSts = 7
sKeyValue = "c:\temp" (after trimming)

IF I attempt to read from the other module using the same code I get:

lRtnSts = 7
sKeyValue = " "
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top