Excel VBA PrivateProfile done right - and easy
Introduction
PrivateProfile is the term used for information in a file organized as
[section]
<valuen-ame>=<value>
structure, typically for config- or ini- files. Word provides for example System.PrivateProfileString with a perfect syntax. Excel unfortunately offers only things like GetPrivateProfileString with a much less comfortable syntax. The Standard Module mFile provides ‘Word-like’ services which as well mainly deal with the arguments: file, section, value-name and value.
PrivateProfile services
Value
Syntax read: value = mFile.Value(file, section, value-name)
Syntax write: mFile.Value(file, section, value-name) = value
ValueExists
Syntax: If mFile.ValueExists(file[, section], value) Then
NameExists
Syntax: If mFile.NameExists(file[, section], value-name) Then
SectionExists
Syntax: If mFile.SectionExists(file, section) Then
SectionsCopy
Syntax: mFile.SectionsCopy source, target, sections
Named arguments
Part | Description | Services |
---|---|---|
pp_file | String expression, obligatory, specifies the full name of the _PrivateProfile file, automatically created with the first write if a named value. | All |
pp_sections | Variant, optional, defaults to ‘all sections in file’ when omitted. Section names may be provided as a comma delimited string, or a Dictionary or Collection of name items. | SectionsCopy SectionsRemove ValueExists ValueNameExists |
pp_replace | Optional, boolean, defaults to false (i.e. the copied section is merged in the target file. | SectionsCopy |
pp_section | Obligatory, String expression, identifies the section | NameRemove SectionExists SectionRemove Value |
pp_source | String expression, obligatory, specifies the full name of the source PrivateProfile file | SectionsCopy |
pp_target | String expression, obligatory, specifies the full name of the target PrivateProfile file | SectionsCopy |
pp_value_name | NameRemove Value |
|
pp_value | Variant expression, the value written to the PrivateProfile file | Value ValueExists |
Installation
Usage
The services may best be used in a Standard Module dedicated to the file used for the application specific values, whereby each value preferably should be implemented as a Property. The following example provides a read service for a property called RootFolder in a module called mCfg.
Option Explicit
Private Const CFG_SECTION = "Basic"
Private Const VAL_NAME_ROOT_FOLDER = "RootFolder"
Private Property Get CfgFile() As String
CfgFile = ...... ' specifying the ProvateProfile file's full name
End Property
Private Property Get RootFolder() As String
RootFolder = mFile.Value(CfgFile, CFG_SECTION, VAL_NAME_ROOT_FOLDER)
End Property
This service will be used subsequently in the project:
sRoot = mCfg.RootFolder
the matter of all the above is a pretty nice example how the implementation of a couple of modules each providing a service on a higher abstraction layer. I.e. each module provides a service while hiding the used technical means.