View Single Post
  #9   Report Post  
Posted to microsoft.public.excel.programming
GS[_2_] GS[_2_] is offline
external usenet poster
 
Posts: 3,514
Default EXCEL 2010 VBA; trying to use INI File stored on local desktop machines

JingleRock pretended :
Progress:

I now believe that Attempt #3 in my original post is the correct way
to go. (mapping to the correct server and to the correct user is
automatically taken care of by Win 7).

However, I still cannot get my macro to recognize the INI File. What
about my question as to:

Yes, you are correct; we are using 'GetPrivateProfileStringA' code
and
'GetPrivateProfileIntA' code. Are there any References to Object
Libraries that we need? << as I key, there are five References --
they
are all pretty basic. I have not converted the .xls extension on the
macro file.

JingleRock


You do need to make the API declarations in the module that uses them.
The ones I use are...

Private Declare Function GetPrivateProfileStringA Lib "kernel32" (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
Private Declare Function GetPrivateProfileIntA Lib "kernel32" (ByVal
lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault
As Long, ByVal lpFileName As String) As Long
Private Declare Function GetPrivateProfileSectionA Lib "kernel32"
(ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal
nSize As Long, ByVal lpFileName As String) As Long
Private Declare Function WritePrivateProfileStringA Lib "kernel32"
(ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal
lpString As String, ByVal lpFileName As String) As Long
Private Declare Function WritePrivateProfileSectionA Lib "kernel32"
(ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName
As String) As Long


Helper declares...

Public Const glNOT_SET As Long = -9999
Public Const glBASIC_STRING_BUFFER As Long = 257
Public Const glHANDLED_ERROR As Long = 9999
Public Const gszEMPTY_STRING As String = ""


To read a key value...

Public Function szGetStringKeyValue(ByVal szFilename As String, ByVal
szSection As String, ByVal szKey As String, Optional ByVal szDefault As
String = vbNullString) As String
'
' Comments: Returns the string value of the specified INI file Key.
'
' Arguments: szFilename [in] The full path and filename of the INI
file to look in.
' szSection [in] The name of the Section in which the
Key to be queried is located.
' szKey [in] The name of the Key to return the
value for.
' szDefault [in] (Optional) The default value to return
if the specified Key is not found in the INI file.
' Default = gszEMPTY_STRING.
'
' Returns: String The value for szKey, or gszEMPTY_STRING on
error.
'
' Date Developer Action
'
--------------------------------------------------------------------------
' 10/10/98 Rob Bovey Created
'

Const sSource As String = "szGetStringKeyValue()"

Dim lReturn As Long
Dim lLength As Long
Dim szKeyBuffer As String

'Set default values
lLength = glBASIC_STRING_BUFFER
szKeyBuffer = String$(lLength, vbNullChar)

TryAgain: 'Execution returns here if the buffer allocation wasn't large
enough.

'Call the API function.
lReturn = GetPrivateProfileStringA(szSection, szKey, szDefault,
szKeyBuffer, lLength, szFilename)

'Return the value from the buffer if a value was located.
If lReturn 0 Then
If lReturn = lLength - 1 Then
'The buffer wasn't large enough to hold the return value,
'increase it and try again.
lLength = lLength * 2
szKeyBuffer = String$(lLength, vbNullChar)
GoTo TryAgain
Else
'Strip the return value out of the buffer and pass it back.
szGetStringKeyValue = Left$(szKeyBuffer, lReturn)
End If
Else
'No value was located.
szGetStringKeyValue = vbNullString
End If

End Function

Public Function lGetNumericKeyValue(ByVal szFilename As String, ByVal
szSection As String, ByVal szKey As String, Optional ByVal lDefault As
Long = glNOT_SET) As Long
'
' Comments: Returns the numeric value of the specified INI file Key.
'
' Arguments: szFilename The full path and filename of the INI file
to look in.
' szSection The name of the Section in which the Key is
located.
' szKey The name of the Key to return the value
for.
' lDefault (Optional) The default value to return if
the specified Key is not found in the INI file.
' Default = glNOT_SET.
'
' Returns: Long The value for szKey, or 0 on error.
'
' Date Developer Action
'
--------------------------------------------------------------------------
' 10/10/98 Rob Bovey Created
'

Const sSource As String = "lGetNumericKeyValue()"

'Call the API function with the specified arguments.
lGetNumericKeyValue = GetPrivateProfileIntA(szSection, szKey,
lDefault, szFilename)

End Function


To write a key value...

Public Function bAddRemoveKeyValue(ByVal szFilename As String, ByVal
szSection As String, ByVal szKey As String, Optional ByVal szValue As
String = vbNullString) As Boolean
'
' Comments: Sets and deletes Key values in INI files.
'
' Arguments: szFilename [in] The full path and filename of the
INI file to use.
' szSection [in] The name of the Section in which the
Key is located.
' szKey [in] The name of the Key to set/delete
the value for.
' szValue [in] (Optional) If passed, this will be
the value set
' for the specified Key (this key will
be added if it does not already exist).
' If not passed, the specified key
will be deleted.
'
' Returns: Boolean True on success, False on error.
'
' Date Developer Action
'
--------------------------------------------------------------------------
' 10/10/98 Rob Bovey Created
' 11/8/2005 Garry Sansom Updated Error Handling
'

Const sSource As String = "bAddRemoveKeyValue()"

Dim lReturn As Long

If gbDEBUG_MODE Then
On Error GoTo 0
Else
On Error GoTo ErrorHandler
End If

If Len(szValue) 0 Then
'Set this value for the specified key.
lReturn = WritePrivateProfileStringA(szSection, szKey, szValue,
szFilename)
Else
'Delete any current value for the specified key.
lReturn = WritePrivateProfileStringA(szSection, szKey,
vbNullString, szFilename)
End If

If lReturn = 0 Then Err.Raise Number:=glHANDLED_ERROR

ErrorExit:
bAddRemoveKeyValue = True
Exit Function

ErrorHandler:
If Len(Err.Description) 0 Then gszErrMsg = Err.Description
If Err.Number < glHANDLED_ERROR Then gszErrMsg = gszErrMsg & "
(bAddRemoveKeyValue)"
bAddRemoveKeyValue = False

' If Err.Number < glHANDLED_ERROR Then Err.Description =
Err.Description & " (" & sSource & ")"
' If bCentralErrorHandler(msModule, sSource) Then
' Stop
' Resume
' Else
' Resume ErrorExit
' End If

End Function

**NOTE 1** I use a central error handling methodology so you may want
to comment out my code for that and/or replace it with your own.

**NOTE 2** This code represents only part of the entire
mPrivateProfileStrings.bas, which was originally provided by the noted
author.

HTH

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc