How to store user settings?

Started by Thomas Winkel, March 06, 2016, 07:38:07 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Thomas Winkel

Hi,

what is the best way to store user settings with VBA?
The code is in a stencil and should read the settings on startup.
At the moment I'm using the data section of the document shape sheet, but now I need global settings.

Thanks,
Thomas

Yacine

Yacine

Thomas Winkel

Thanks, Yacine.

No, it's not about menus.
It's user settings like:
* snapToGrid=true
* language="german"
* linkToXYZ="\\blub.de\root\files\XYZ.xlsx"

I could store this in a simple text file, or in an .INI file.
Word can handle ini files with System.PrivateProfileString.

But maybe there are better ways, Windows registry, etc... I don't know.

Schöne Grüße,
Thomas

Yacine

#3
The registry is certainly a good place to look for, but that's an area where I have not yet dared to play ;) .

 
PS: thought again about it and realized that I faced the same problem some years ago.
My lazy solution was to hard code all the settings.
This assumes of course, that you have the same settings for all users (I think to remember that you have such an environment )
Yacine

AndyW

In the past I would have gone for storing that in the registry, but given the way things have changed with later version of Windows, I would choose to use an XML file and use either CSIDL_COMMON_APPDATA it it is to be shared bewteen users or CSIDL_LOCAL_APPDATA if it is user specific, with company/application subfolder.


Public Enum CSIDL_VALUES
    CSIDL_DESKTOP = &H0
    CSIDL_INTERNET = &H1
    CSIDL_PROGRAMS = &H2
    CSIDL_CONTROLS = &H3
    CSIDL_PRINTERS = &H4
    CSIDL_PERSONAL = &H5
    CSIDL_FAVORITES = &H6
    CSIDL_STARTUP = &H7
    CSIDL_RECENT = &H8
    CSIDL_SENDTO = &H9
    CSIDL_BITBUCKET = &HA
    CSIDL_STARTMENU = &HB
    CSIDL_MYDOCUMENTS = &HC
    CSIDL_MYMUSIC = &HD
    CSIDL_MYVIDEO = &HE
    CSIDL_DESKTOPDIRECTORY = &H10
    CSIDL_DRIVES = &H11
    CSIDL_NETWORK = &H12
    CSIDL_NETHOOD = &H13
    CSIDL_FONTS = &H14
    CSIDL_TEMPLATES = &H15
    CSIDL_COMMON_STARTMENU = &H16
    CSIDL_COMMON_PROGRAMS = &H17
    CSIDL_COMMON_STARTUP = &H18
    CSIDL_COMMON_DESKTOPDIRECTORY = &H19
    CSIDL_APPDATA = &H1A
    CSIDL_PRINTHOOD = &H1B
    CSIDL_LOCAL_APPDATA = &H1C
    CSIDL_ALTSTARTUP = &H1D
    CSIDL_COMMON_ALTSTARTUP = &H1E
    CSIDL_COMMON_FAVORITES = &H1F
    CSIDL_INTERNET_CACHE = &H20
    CSIDL_COOKIES = &H21
    CSIDL_HISTORY = &H22
    CSIDL_COMMON_APPDATA = &H23
    CSIDL_WINDOWS = &H24
    CSIDL_SYSTEM = &H25
    CSIDL_PROGRAM_FILES = &H26
    CSIDL_MYPICTURES = &H27
    CSIDL_PROFILE = &H28
    CSIDL_SYSTEMX86 = &H29
    CSIDL_PROGRAM_FILESX86 = &H2A
    CSIDL_PROGRAM_FILES_COMMON = &H2B
    CSIDL_PROGRAM_FILES_COMMONX86 = &H2C
    CSIDL_COMMON_TEMPLATES = &H2D
    CSIDL_COMMON_DOCUMENTS = &H2E
    CSIDL_COMMON_ADMINTOOLS = &H2F
    CSIDL_ADMINTOOLS = &H30
    CSIDL_CONNECTIONS = &H31
    CSIDL_COMMON_MUSIC = &H35
    CSIDL_COMMON_PICTURES = &H36
    CSIDL_COMMON_VIDEO = &H37
    CSIDL_RESOURCES = &H38
    CSIDL_RESOURCES_LOCALIZED = &H39
    CSIDL_COMMON_OEM_LINKS = &H3A
    CSIDL_CDBURN_AREA = &H3B
    CSIDL_COMPUTERSNEARME = &H3D
    CSIDL_FLAG_PER_USER_INIT = &H800
    CSIDL_FLAG_NO_ALIAS = &H1000
    CSIDL_FLAG_DONT_VERIFY = &H4000
    CSIDL_FLAG_CREATE = &H8000
    CSIDL_FLAG_MASK = &HFF00
End Enum

Public Function GetFolderPath( _
    csidl As Long, _
    SHGFP_TYPE As Long) As String

    Dim buff As String
    Dim dwFlags As Long
    Dim lngStatus As Long
 
    'fill buffer with the specified folder item
    buff = Space$(MAX_LENGTH)
   
    lngStatus = SHGetFolderPath( _
                        0, _
                        csidl Or dwFlags, _
                        -1, _
                        SHGFP_TYPE, _
                        buff)
                     
    If lngStatus = S_OK Then
                     
        GetFolderPath = Left$(buff, InStr(buff, Chr$(0)) - 1)
       
    End If
   
End Function
Live life with an open mind

vojo

Unlike 2003, 2013 doesn't seem to remember settings.  I ended up making a template in order to capture the settings.

Yacine

Quote from: vojo on March 07, 2016, 01:55:58 PM
Unlike 2003, 2013 doesn't seem to remember settings.  I ended up making a template in order to capture the settings.
I did the same. The things that could not be stored in the template had to be set by code (hard).
Yacine

Thomas Winkel

Hi,

sorry for the late reply.

@Yacine:
Hard coding is not an option because I need individual and dynamic settings that can be changed by the user.
For example I have a checkbox "Snap to grid" in my ribbon menu.
At the moment this setting is stored in the in the document shape sheet.
When another user opens this document these settings are imposed on him.
When the original user opens another document he also opens different settings.
"Snap to grid" is not so fatal, but I also have settings like "Link to my time recording".

@Andy:
Thanks, CSIDL_LOCAL_APPDATA looks interesting. Also I will have a look at XML and how to read /write it with VBA.

@vojo:
I think some visio settings are stored in the document and other ones are global in the registry.
I have a small tool that helps users to set-up recommended visio settings.
But this is only about specific settings that come with my code.

Regards,
Thomas

Nikolay

VBA has a set of built-in functions to manage user settings...

Check these: SaveSetting, GetSetting, etc..
https://msdn.microsoft.com/en-us/library/office/gg278430.aspx

Thomas Winkel