EXCEL 2010 VBA; trying to use INI File stored on local desktop machines

  • Thread starter Thread starter JingleRock
  • Start date Start date
J

JingleRock

We are migrating from XCL 2003 to XCL 2010. The macro file resides on
a shared network drive. I am having difficulty getting the macro code
to "recognize" (extracting email parameters from the INI file; to be
used for the macro to send an email) the INI file. Each user has his
customized INI file stored on his H:\ drive.

These are my five unsuccessful attempts:

1) Public Const INI_PATH_TMPL = "H:\<USER_ID>\My Documents\INI File\"

2) Public Const INI_PATH_TMPL = "\\ServerName\<USER_ID>\My Documents
\INI File\"

where:
'sUserID = (Environ$("Username")) '<< THIS IS THE WIN 7 CODE FOR
ANY GIVEN USER

'GetUserID = Replace(sUserID, "corp\", "")

'GetINIFilename = Replace(APP_INI_PATH, "<USER_ID>", GetUserID)

3) Public Const INI_PATH_TMPL = "H:\My Documents\INI File\"

4) Public Const INI_PATH_TMPL = "H:\loginID\My Documents\INI File\"

5) Public Const APP_INI_PATH = INI_PATH_TMPL & "Email.ini"

Please help.
 
We are migrating from XCL 2003 to XCL 2010. The macro file resides on
a shared network drive. I am having difficulty getting the macro code
to "recognize" (extracting email parameters from the INI file; to be
used for the macro to send an email) the INI file. Each user has his
customized INI file stored on his H:\ drive.

These are my five unsuccessful attempts:

1) Public Const INI_PATH_TMPL = "H:\<USER_ID>\My Documents\INI File\"

2) Public Const INI_PATH_TMPL = "\\ServerName\<USER_ID>\My Documents
\INI File\"

where:
    'sUserID = (Environ$("Username")) '<< THIS IS THE WIN 7 CODE FOR
ANY GIVEN USER

    'GetUserID = Replace(sUserID, "corp\", "")

    'GetINIFilename = Replace(APP_INI_PATH, "<USER_ID>", GetUserID)

3) Public Const INI_PATH_TMPL = "H:\My Documents\INI File\"

4) Public Const INI_PATH_TMPL = "H:\loginID\My Documents\INI File\"

5) Public Const APP_INI_PATH = INI_PATH_TMPL & "Email.ini"

Please help.

Actually, #5 is NOT an unsuccessful attempt; just indicates the INI
Filename string.
 
INI files are usually managed using the PrivateProfileStrings APIs.
There's no reason I can see why code that worked in xl2003 shouldn't
work in xl2010 unless you were using some other dubious method.
 
INI files are usually managed using the PrivateProfileStrings APIs.
There's no reason I can see why code that worked in xl2003 shouldn't
work in xl2010 unless you were using some other dubious method.

--
Garry

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

Thanks Garry.

A couple of things I failed to mention: at the same time we upgraded
OFFICE, we went to WIN 7; and, for each NW user, his/her 'My
Documents' folder is on the H:\ drive, instead of on the C:\drive.

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.

This code has been working fine for two years using xl2003 VBA. Also,
when I "hard-code" the email parameters (as opposed to extracting them
from the INI file) into the VBA code, the email is sent as directed.
Also, (Environ$("Username")) is working fine (no hard-coding).

Thanks for your help,
JingleRock
 
Thanks Garry.

A couple of things I failed to mention:  at the same time we upgraded
OFFICE, we went to WIN 7; and, for each NW user, his/her 'My
Documents' folder is on the H:\ drive, instead of on the C:\drive.

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.

This code has been working fine for two years using xl2003 VBA. Using xl2010 VBA,
when I "hard-code" the email parameters (as opposed to extracting them
from the INI file) into the VBA code, the email is sent as directed.
Also,  (Environ$("Username")) is working fine (no hard-coding).

Thanks for your help,
JingleRock

ADDITIONAL INPUT BY JingleRock:

Attempt #3 is most consistent w/ clicking on 'Start' and then
'searching programs and files'; also, it is most consistent w/ xl2003
coding ("C:\Documents and Settings\<USER_ID>\My Documents\INI File\").
Attempt #2 makes the most sense, I guess, but it is not consistent w/
clicking on 'Start' and then 'searching programs and files'.

JingleRock
 
JingleRock wrote on 8/16/2011 :
ADDITIONAL INPUT BY JingleRock:

Attempt #3 is most consistent w/ clicking on 'Start' and then
'searching programs and files'; also, it is most consistent w/ xl2003
coding ("C:\Documents and Settings\<USER_ID>\My Documents\INI File\").
Attempt #2 makes the most sense, I guess, but it is not consistent w/
clicking on 'Start' and then 'searching programs and files'.

JingleRock

Sounds to me like Win7 access permissions may be at play here. The user
must have read/write permission to the location of the INI file.
Normally, this should work fine when files are stored under ~Documents
and Settings\<userid>\My Documents\ or any of its subfolders.
 
Sounds to me like Win7 access permissions may be at play here. The user
must have read/write permission to the location of the INI file.
Normally, this should work fine when files are stored under ~Documents
and Settings\<userid>\My Documents\ or any of its subfolders.

Since I created the 'INI File' folder and the INI file in it, I don't
think that is an issue.
And no 'Documents and Settings' folder, now.
 
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
 
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
 
Thanks for all the great code, Garry. It will take a while for me to
work through it.
What is the initial value of 'ILength' that you use?

JingleRock
 
JingleRock explained on 8/18/2011 :
Thanks for all the great code, Garry. It will take a while for me to
work through it.
What is the initial value of 'ILength' that you use?

JingleRock

lLength is initialized with the declared constant glBASIC_STRING_BUFFER
in the szGetStringKeyValue() function. This is one of the 4 helper
declares to be pasted below the API declares at the top of the module.
 
Back
Top