Saving to and retrieving from the registry :: MrExcel Message Board


 FAQFAQ
   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   FavoritesFavorites   StatisticsStatistics 
 RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
Online StoreOnline Store

MrExcel Message Board Forum Index -> Excel Questions

Saving to and retrieving from the registry
Post new topic   Reply to topic
Last Thread | Next Thread  >   Printable version
  Author    Thread

Maggi
Board Regular


Joined: 19 Jun 2003
Posts: 25

Flag: Blank

Status: Offline

 Reply with quote  

Saving to and retrieving from the registry

The following code is supposed to save status of option buttons and check boxes in the current sheet into the registry on the click of a button:

code:
Sub CommandButton2_Click()
    Dim ctrl As OLEObject
    Dim CtrlType As String
    For Each ctrl In Me.OLEObjects
        CtrlType = TypeName(ctrl)
        If CtrlType = "CheckBox" Or CtrlType = "OptionButton" Then
            SaveSetting APPNAME, "Defaults", ctrl.Name, ctrl.Value
        End If
    Next ctrl
    MsgBox "Current settings have been saved as default!"
End Sub


And then whenever the file is opened, the following code is supposed to retrieve the saved defaults from the registry:
code:
Private Sub Workbook_Open()
    Dim ctrl As OLEObject
    Dim CtrlType As String
    For Each ctrl In Me.OLEObjects   
        CtrlType = TypeName(ctrl)       
        If CtrlType = "CheckBox" Or CtrlType = "OptionButton" Then
            ctrl.Value = GetSetting(APPNAME, "Defaults", ctrl.Name, ctrl.Value)
        End If
    Next ctl
End Sub


But, as usually ( icon_huh.gif ) it doesn't work, although there are no error messages.
Any ideas would be much appreciated...

Post Thu Jul 10, 2003 12:39 pm 
 View user's profile Send private message

Andrew Poulsom
MrExcel MVP


Joined: 22 Jul 2002
Posts: 8244

Flag: Uk

Status: Offline

 Reply with quote  

Re: Saving to and retrieving from the registry

I expect your problem is the use of the Me keyword in your Workbook_Open event procedure. In that context Me is the Workbook not the Worksheet containing your controls. So you need something like:

For Each ctrl In Me.Worksheets("Sheet1").OLEObjects

Post Thu Jul 10, 2003 12:49 pm 
 View user's profile Send private message

Maggi
Board Regular


Joined: 19 Jun 2003
Posts: 25

Flag: Blank

Status: Offline

 Reply with quote  

Re: Saving to and retrieving from the registry

Ok! But for some reason the defaults don't get saved in the registry in the first place.

Post Thu Jul 10, 2003 2:22 pm 
 View user's profile Send private message

Andrew Poulsom
MrExcel MVP


Joined: 22 Jul 2002
Posts: 8244

Flag: Uk

Status: Offline

 Reply with quote  

Re: Saving to and retrieving from the registry

What's APPNAME?

Post Thu Jul 10, 2003 2:27 pm 
 View user's profile Send private message

Juan Pablo González
MrExcel MVP


Joined: 09 Feb 2002
Posts: 8536
Location: Marion, IL
Flag: Colombia

Status: Offline

 Reply with quote  

Re: Saving to and retrieving from the registry

Is SaveSetting working ? I mean, do you see the entry under VB & VBA Program Settings ?

If that works, the problem that I see with GetSetting is that it returns a string, "True" or "False" which are NOT the boolean values that the checkbox would expect...
_________________
Regards,

Juan Pablo González
MrExcel.com Consulting

Read the Articles List and check out our Recommended links and Add-Ins

Post Thu Jul 10, 2003 2:32 pm 
 View user's profile Send private message Visit poster's website

Maggi
Board Regular


Joined: 19 Jun 2003
Posts: 25

Flag: Blank

Status: Offline

 Reply with quote  

Re: Saving to and retrieving from the registry

quote:
Originally posted by Andrew Poulsom:
What's APPNAME?
AFAIK, it's a global constant that holds the application name.


quote:
Originally posted by Juan Pablo González:
Is SaveSetting working ? I mean, do you see the entry under VB & VBA Program Settings ?
Nope, that's the problem (for now), can't find it in the registry...

Post Thu Jul 10, 2003 3:19 pm 
 View user's profile Send private message

Juan Pablo González
MrExcel MVP


Joined: 09 Feb 2002
Posts: 8536
Location: Marion, IL
Flag: Colombia

Status: Offline

 Reply with quote  

Re: Saving to and retrieving from the registry

APPNAME needs to be defined somewhere. It's not "builtin", like you could use in VB

App.Title

APPNAME should be in a standard module like

Public Const APPNAME As String = "My Title"

And maybe its not saving anything in the registry because, as pointed by Andre, its not going inside the loop ?
_________________
Regards,

Juan Pablo González
MrExcel.com Consulting

Read the Articles List and check out our Recommended links and Add-Ins

Post Thu Jul 10, 2003 3:22 pm 
 View user's profile Send private message Visit poster's website

Andrew Poulsom
MrExcel MVP


Joined: 22 Jul 2002
Posts: 8244

Flag: Uk

Status: Offline

 Reply with quote  

Re: Saving to and retrieving from the registry

[quote="Maggi"]AFAIK, it's a global constant that holds the application name.

What do you get if you type?

MsgBox APPNAME

in the Immediate window?

Post Thu Jul 10, 2003 3:23 pm 
 View user's profile Send private message
  Display posts from previous:      

MrExcel Message Board Forum Index -> Excel Questions


Forum Jump:
Jump to:  

Post new topic   Reply to topic
Page 1 of 1



Add To Favorites

 


Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Powered by phpBB: 2.0.4 © 2001 phpBB Group

Need help posting your first question? Read how to post

Need extra help ? Couldn't get the answer you needed ? Get a free quote from our Consulting Team

Download Colo's HTML Maker utility for displaying your Excel Worksheet on the board.

Download VB HTML Maker to post your code on the board


Check out our new index to 485 Excel Articles.


Return to MrExcel Consulting

All contents Copyright 1998-2004 by MrExcel.com
If you believe information posted here is from your copyrighted source, notify us per the Terms of Use
Excel is a registered trademark of the Microsoft Corporation.
MrExcel is a registered trademark of Tickling Keys, Inc.