PDA

View Full Version : Solved: Creating a config file.



BlueDNA
04-20-2006, 04:54 PM
Hi all,

Can someone tell me Excel's equivalent to:


Dim strResult As String
Dim fileName As String

fileName = "C:\config.ini"

strResult = WordBasic.[GetPrivateProfileString$]("Username", "Name", fileName)



I would like to create a config file and would like to use a similar method (predefined tags) instead of reading the whole file.

Is this even possible? :dunno

Thanks in advance for any help or suggestions!

David

Edit:

Ive found a Windows API that does this, but im having difficulty getting it to work. The file exists, but the section and key fail. Therefore the default value is always returned instead.


Option Explicit
#If Win32 Then
Private Declare Function GetPrivateProfileString Lib _
"kernel32" Alias "GetPrivateProfileStringA" _
(ByVal lpApplicationName As String, lpKeyName As Any, ByVal _
lpDefault As String, ByVal lpRetStr As String, ByVal nSize _
As Long, ByVal lpFileName$) As Long
Private Declare Function FindWindow Lib "user32" _
Alias "FindWindowA" (ByVal lpClassName As Any, _
ByVal lpWindowName As Any) As Long
Private Declare Function PostMessage Lib "user32" Alias _
"PostMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lParam As Any) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hWnd&, _
ByVal nCmdShow As Long) As Long
#Else
Private Declare Function GetPrivateProfileString Lib "Kernel" _
(ByVal lpAppName$, ByVal lpKeyName$, ByVal lpDefault$, _
ByVal lpReturnStr$, ByVal nSize%, ByVal lpFileName$) _
As Integer
Private Declare Function FindWindow Lib "User" _
(ByVal lpClassName$, ByVal lpWindowName As Long) As Integer
Private Declare Function PostMessage Lib "User" (ByVal hWnd%, _
ByVal wMsg As Integer, ByVal wParam%, lParam&) As Long
Private Declare Function ShowWindow Lib "User" _
(ByVal hWnd As Integer, ByVal nCmdShow As Integer) As Integer
#End If

Public Function GetINI(ByVal Section$, ByVal Key$, ByVal _
Default$, ByVal FileName$) As String Dim res%, retVal$
retVal = Space$(32400)
res = GetPrivateProfileString(Section, Key, Default, _ retVal, Len(retVal), FileName)
GetINI = Left$(retVal, res)
End Function


This is what im trying:


Dim configFile As String
Dim strResult As String
configFile = "C:\config.ini"
strResult = GetINI("Username", "Name", "Not Found", configFile)
MsgBox strResult


Any ideas?

Jan Karel Pieterse
04-21-2006, 01:25 AM
Why not use the registry:

www.jkp-ads.com/articles/distributemacro08.htm (http://www.jkp-ads.com/articles/distributemacro08.htm)

Killian
04-21-2006, 01:49 AM
In the Win32 function declaration for GetPrivateProfileString, there's a missing "ByVal" before "lpKeyName As Any"

Bob Phillips
04-21-2006, 03:18 AM
I posted an example in th NGs a while back at http://tinyurl.com/js757

BlueDNA
04-23-2006, 04:41 PM
Thanks guys for the replies.

Got it working.

Registry is an interesting option!!

Appreciate all your help! Keep up the good work!!

Thanks again! :thumb

geekgirlau
04-26-2006, 07:25 PM
Hi David,

I've marked this thread as solved for you - let me know if I've jumped the gun!