Who do you read an ini file with Excel VBA?

W

Webtechie

Hello,

I need to have a couple of offices use an application. The front end is
written in Excel with Userforms and VBA. The back end is SQL Server.

One office is on site. The other office is in another building. I need to
create a DSN for that office that can see the data across the internet. But
I also need to create an ini file so that the application can retrieve a
value and know whether to access the data directly or use a dsn.

Question
======
What is the code to read an ini from within Excel VBA?


Thanks,

Tony
 
M

Mike H

Hi,

This would read an .ini file line by line. The message box isn't necessary,
I put it there to demonstrate it works.

Sub Read_INI()
saveDir = "C:\" 'Place where your INI file is
filenum = FreeFile
targetfile = saveDir & "Myfile.ini" 'Change to your filename
Open targetfile For Input As filenum
Do While Not EOF(filenum)
Input #filenum, Line$
MsgBox Line$
Loop
Close #filenum
End Sub
--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.
 
J

JLatham

Typically a .ini file is just a simple ASCII text file. The trick to reading
one is to know what you expect to find in it, where to find it and how to
parse it. Here are the basic pieces you'll need. I have one line in it that
simply echos the .ini file contents to the currently active sheet - that can
help while you're trying to code up the parsing. Look at the Instr()
function in VB, it can help with the parsing, and you'll probably end up
using Mid(), Left() and/or Right().

Sub INI_FileReading()
Dim myIniFile As String
Dim iniBuff As Integer
Dim rawLineInput As String

'set up a path to your .ini file
myIniFile = "x:\folder\folder\theFile.ini"
iniBuff = Freefile() ' get available file buffer number
Open myIniFile For Input As #iniBuff

Do While Not (EOF(iniBuff))
Line Input #iniBuff, rawLineInput ' get one line from the file
'next line will show you what's being read from the file
ActiveSheet.Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = rawLineInput
'parse the lines of data here and save any values you need to use later
Loop
Close #iniBuff

End Sub
 
R

RB Smissaert

To read or write to an .ini you need to use the API as there can be a
problem
to do with the Windows cache when you open the file as described in the
previous posts.
There is code on Chip Pearson's site that should do the job or you could use
something like this:

Option Explicit
Private 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


Function ReadINIValue(strINIPath As String, _
vSection As Variant, _
vKey As Variant, _
Optional vDefault As Variant = "<no value>") As String

Dim buf As String * 256
Dim Length As Long

On Error GoTo ERROROUT

If bFileExists(strINIPath) = False Then
ReadINIValue = "<no file>"
Exit Function
End If

Length = GetPrivateProfileString(vSection, _
vKey, _
vDefault, _
buf, _
Len(buf), _
strINIPath)

ReadINIValue = Left$(buf, Length)

Exit Function
ERROROUT:

ReadINIValue = "<error>"

End Function


Function bFileExists(strFile As String) As Boolean

Dim lAttr As Long

On Error Resume Next

lAttr = GetAttr(strFile)
bFileExists = (Err.Number = 0) And ((lAttr And vbDirectory) = 0)

End Function


RBS
 

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