Reading from ini file

T

Tammy

I need to create a macro in Excel which reads from an ini file. Word
uses the system.privateprofilestring but I can't find anything like
this in Excel. Any suggestions greatly appreciated!
 
C

Colo

Tammy, :)
Because an ini file is just a text file, it can be read with Ope
statment.

Code
-------------------

Sub test()
Dim fn As Long
Dim tmp As String
fn = FreeFile
'Change here to the file path
Open "c:\test.ini" For Input As fn
Do While Not EOF(fn)
Line Input #fn, tmp
Debug.Print tmp
Loop
Close fn
End Sub
 
A

Andy Wiggins

You need to use API calls.

1) Copy the code, below, into a module
2) Run the Sub "aIniWriteTest" : if it does not exist, this will create the
INI file and write a value to it.
3) Run the sSub "aIniReadTest" : this will read the INI file and report back
the value written to it in (2)

'' - - - - - - - Code Starts
Option Explicit

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
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias
"WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal
lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long

Private Const c_ini_file_name = "Test.Ini"
Private c_WinDir As String
Private c_profile_string As String
Private c_ini_string As String
Private c_ini_no_of_chars As Integer

''
***************************************************************************
Sub aIniWriteTest()
WriteIniFileString "Test", "SubTest", 99
End Sub

''
***************************************************************************
Sub aIniReadTest()
MsgBox ReadIniFileString("Test", "SubTest")
End Sub

''
***************************************************************************
Public Function ReadIniFileString(ByVal Sect As String, ByVal Keyname As
String) As String

Dim worked As Long
Dim RetStr As String * 128
Dim StrSize As Long

c_ini_no_of_chars = 0
c_ini_string = ""

If Sect = "" Or Keyname = "" Then
MsgBox "Section Or Key To Read Not Specified !!!", vbExclamation,
"INI"
Else
c_profile_string = ""
RetStr = Space(128)
StrSize = Len(RetStr)

worked = GetPrivateProfileString(Sect, Keyname, "", RetStr, StrSize,
ThisWorkbook.path & "\" & c_ini_file_name)
If worked Then
c_ini_no_of_chars = worked
c_ini_string = Left$(RetStr, worked)
End If
End If
ReadIniFileString = c_ini_string

End Function

''
***************************************************************************
Public Function WriteIniFileString(ByVal Sect As String, ByVal Keyname As
String, ByVal Wstr As String) As String

Dim worked As Long

c_ini_no_of_chars = 0
c_ini_string = ""

If Sect = "" Or Keyname = "" Then
MsgBox "Section Or Key To Write Not Specified !!!", vbExclamation,
"INI"
Else
worked = WritePrivateProfileString(Sect, Keyname, Wstr,
ThisWorkbook.path & "\" & c_ini_file_name)
If worked Then
c_ini_no_of_chars = worked
c_ini_string = Wstr
End If

WriteIniFileString = c_ini_string
End If

End Function

'' - - - - - - - Code Ends

--
Regards
Andy Wiggins
www.BygSoftware.com
Home of "Save and BackUp",
"The Excel Auditor" and "Byg Tools for VBA"
 
B

Bob Phillips

Tammy,

In Excel you have to use the GetPrivateProfileString,
WritePrivateProfileString APIs, so requires much more coding. I have a demo
somewhere (using CDPlayer.ini), would you like me to dig it out?

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
T

Tammy

That would be great! Thanks Bob.

Bob Phillips said:
Tammy,

In Excel you have to use the GetPrivateProfileString,
WritePrivateProfileString APIs, so requires much more coding. I have a demo
somewhere (using CDPlayer.ini), would you like me to dig it out?

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top