Option group needs to show current value

  • Thread starter christophercbrewster via OfficeKB.com
  • Start date
C

christophercbrewster via OfficeKB.com

I have some option groups on a form. A group sets a DocVariable value, and I
need the group to show the current setting (the button for the selected value
shown as selected). I would have thought this was an easily accessible, built-
in capability of option groups, but I haven't found an explanation of how to
do it. Would appreciate suggestions or links.
 
D

Doug Robbins - Word MVP

Private Sub OptionButton1_Click()
If OptionButton1.Value = True Then ActiveDocument.Variables("Test") = 1
End Sub
Private Sub OptionButton2_Click()
If OptionButton1.Value = True Then ActiveDocument.Variables("Test") = 2
End Sub
Private Sub OptionButton3_Click()
If OptionButton1.Value = True Then ActiveDocument.Variables("Test") = 3
End Sub

Then, in the routine the running of which you want to set the Option button
according to the value of the document variable, use

Select Case ActiveDocument.Variables("Test").Value
Case 1
OptionButton1.Value = True
OptionButton2.Value = False
OptionButton3.Value = Flase
Case 2
OptionButton1.Value = False
OptionButton2.Value = True
OptionButton3.Value = Flase
Case 3
OptionButton1.Value = False
OptionButton2.Value = False
OptionButton3.Value = True
End Select


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
C

christophercbrewster via OfficeKB.com

I tried applying your code to what I have. My version appears logically the
same as yours, but it has no effect. The following is just a little
simplified, showing only one of my option groups. If you can point out any
errors you see, I'd appreciate it.

Notes:
- This userform already works correctly, and it even leaves the user's
choices showing in the option groups, but they're gone when I re-open the
document.
- The DocVariable shown here uses string values for true and false.

In the userform code:
----------------
' Defining the group:
Private Sub UserForm_Initialize()
YesOption.GroupName = "InclTitle"
NoOption.GroupName = "InclTitle"
End Sub
-----------------
' "Yes" button:
Private Sub YesOption_Click()
ActiveDocument.Variables("include-title").Value = "True"
End Sub
-----------------
' "No" button:
Private Sub NoOption_Click()
ActiveDocument.Variables("include-title").Value = "False"
End Sub

--------------------------------------
' Part of the routine that calls the userform.

' Read include-title option, which might not be set yet:
On Error Resume Next
sTitleOption = ActiveDocument.Variables("include-title").Value
If Err.Number <> 0 Then ' If it's not set, set it to "True" string
value
ActiveDocument.Variables("include-title").Value = "True"
sTitleOption = "True"
End If

Select Case sTitleOption
Case "True"
YesOption.Value = True
NoOption.Value = False
Case "False"
YesOption.Value = False
NoOption.Value = True
End Select

Private Sub OptionButton1_Click()
If OptionButton1.Value = True Then ActiveDocument.Variables("Test") = 1
End Sub
Private Sub OptionButton2_Click()
If OptionButton1.Value = True Then ActiveDocument.Variables("Test") = 2
End Sub
Private Sub OptionButton3_Click()
If OptionButton1.Value = True Then ActiveDocument.Variables("Test") = 3
End Sub

Then, in the routine the running of which you want to set the Option button
according to the value of the document variable, use

Select Case ActiveDocument.Variables("Test").Value
Case 1
OptionButton1.Value = True
OptionButton2.Value = False
OptionButton3.Value = Flase
Case 2
OptionButton1.Value = False
OptionButton2.Value = True
OptionButton3.Value = Flase
Case 3
OptionButton1.Value = False
OptionButton2.Value = False
OptionButton3.Value = True
End Select
I have some option groups on a form. A group sets a DocVariable value, and
I
[quoted text clipped - 5 lines]
to
do it. Would appreciate suggestions or links.
 
D

Doug Robbins - Word MVP

I think that your problem may be in the following part of your code:

Private Sub UserForm_Initialize()
YesOption.GroupName = "InclTitle"
NoOption.GroupName = "InclTitle"
End Sub



--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

christophercbrewster via OfficeKB.com said:
I tried applying your code to what I have. My version appears logically the
same as yours, but it has no effect. The following is just a little
simplified, showing only one of my option groups. If you can point out any
errors you see, I'd appreciate it.

Notes:
- This userform already works correctly, and it even leaves the user's
choices showing in the option groups, but they're gone when I re-open the
document.
- The DocVariable shown here uses string values for true and false.

In the userform code:
----------------
' Defining the group:
Private Sub UserForm_Initialize()
YesOption.GroupName = "InclTitle"
NoOption.GroupName = "InclTitle"
End Sub
-----------------
' "Yes" button:
Private Sub YesOption_Click()
ActiveDocument.Variables("include-title").Value = "True"
End Sub
-----------------
' "No" button:
Private Sub NoOption_Click()
ActiveDocument.Variables("include-title").Value = "False"
End Sub

--------------------------------------
' Part of the routine that calls the userform.

' Read include-title option, which might not be set yet:
On Error Resume Next
sTitleOption = ActiveDocument.Variables("include-title").Value
If Err.Number <> 0 Then ' If it's not set, set it to "True" string
value
ActiveDocument.Variables("include-title").Value = "True"
sTitleOption = "True"
End If

Select Case sTitleOption
Case "True"
YesOption.Value = True
NoOption.Value = False
Case "False"
YesOption.Value = False
NoOption.Value = True
End Select

Private Sub OptionButton1_Click()
If OptionButton1.Value = True Then ActiveDocument.Variables("Test") = 1
End Sub
Private Sub OptionButton2_Click()
If OptionButton1.Value = True Then ActiveDocument.Variables("Test") = 2
End Sub
Private Sub OptionButton3_Click()
If OptionButton1.Value = True Then ActiveDocument.Variables("Test") = 3
End Sub

Then, in the routine the running of which you want to set the Option
button
according to the value of the document variable, use

Select Case ActiveDocument.Variables("Test").Value
Case 1
OptionButton1.Value = True
OptionButton2.Value = False
OptionButton3.Value = Flase
Case 2
OptionButton1.Value = False
OptionButton2.Value = True
OptionButton3.Value = Flase
Case 3
OptionButton1.Value = False
OptionButton2.Value = False
OptionButton3.Value = True
End Select
I have some option groups on a form. A group sets a DocVariable value,
and
I
[quoted text clipped - 5 lines]
to
do it. Would appreciate suggestions or links.
 
C

christophercbrewster via OfficeKB.com

Aside from the trying showing the current value, the option group works
correctly, so I'm looking at other things. I'm wondering if I need to declare
YesOption and the others as objects in the calling routine, because I'm not
getting a value from these (for example, in a message box following the
assignments below). But does the userform code "know" about an object that is
declared in the main code? It doesn't seem to pick up variables declared
public in the main code.
I think that your problem may be in the following part of your code:

Private Sub UserForm_Initialize()
YesOption.GroupName = "InclTitle"
NoOption.GroupName = "InclTitle"
End Sub
I tried applying your code to what I have. My version appears logically the
same as yours, but it has no effect. The following is just a little
[quoted text clipped - 81 lines]
 
C

christophercbrewster via OfficeKB.com

OK, I got it. The code that's similar to what you showed for the calling
routine, I moved to the userform initialization, and it now shows the current
value correctly. Thanks for the code.
Aside from the trying showing the current value, the option group works
correctly, so I'm looking at other things. I'm wondering if I need to declare
YesOption and the others as objects in the calling routine, because I'm not
getting a value from these (for example, in a message box following the
assignments below). But does the userform code "know" about an object that is
declared in the main code? It doesn't seem to pick up variables declared
public in the main code.
I think that your problem may be in the following part of your code:
[quoted text clipped - 8 lines]

--
Christopher Brewster
Lockheed Martin, Eagan MN

Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/200901/1
 
C

christophercbrewster via OfficeKB.com

I previously wrote that I had this solved, but I don't. I've tried putting my
code in both UserForm_Initialize and UserForm_Activate. It doesn't
consistently use the default value set on the form definition *or* the
current value set in the document. It often uses the previous setting for the
form, even if I have closed Word and opened a different document. My co-
worker thinks that the button values are defined at Open and can't be reset
when UserForm.show is invoked. There are effects that I'm having trouble
tracing.

As to your suggestion....
I think that your problem may be in the following part of your code:

Private Sub UserForm_Initialize()
YesOption.GroupName = "InclTitle"
NoOption.GroupName = "InclTitle"
End Sub

As I said, this worked correctly as far as it went. (The two buttons worked
as a group.) How does this affect the issue of showing current settings?
Thanks for any help.

--
Christopher Brewster
Lockheed Martin, Eagan MN

Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/200901/1
 
D

Doug Robbins - Word MVP

You are assigning the same name to what I imagine are different groups?

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
C

christophercbrewster via OfficeKB.com

No, what I show is one group, Fonts. There happens to be another group,
InclTitle, so what I actually have is:

BoldOption.GroupName = "Fonts"
MatchTextOption.GroupName = "Fonts"
RegularOption.GroupName = "Fonts"

YesOption.GroupName = "InclTitle"
NoOption.GroupName = "InclTitle"

These groups work correctly as "radio buttons", so that clicking an option
unselects the previous selection in the group.

As long as you're reading, I just discovered a new issue. Let's say I open
Word by opening PubA.doc. Auto_Exec runs and creates the toolbar. Then I open
PubB.doc. When I run a function from the toolbar of PubB.doc, the focus goes
back to PubA.doc and the function acts on PubA.doc. So the toolbar seems to
apply only to the first document I opened. Does this sound familiar?

I previously wrote that I had this solved, but I don't. I've tried putting my
code in both UserForm_Initialize and UserForm_Activate. It doesn't
consistently use the default value set on the form definition *or* the
current value set in the document. It often uses the previous setting for the
form, even if I have closed Word and opened a different document. My co-
worker thinks that the button values are defined at Open and can't be reset
when UserForm.show is invoked. There are effects that I'm having trouble
tracing.

As to your suggestion....


As I said, this worked correctly as far as it went. (The two buttons worked
as a group.) How does this affect the issue of showing current settings?
Thanks for any help.

--
Christopher Brewster
Lockheed Martin, Eagan MN

Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/200901/1
 
C

christophercbrewster via OfficeKB.com

I think I've been looking at the wrong problem. It's really the new issue I
raised. Pertaining to the original issue, I found a minor error that was
creating a problem. This combined with the document focus changing
(apparently without my noticing it) was giving me weird results. So my real
problem is the document focus. Thanks for your patience, and would like any
suggestions about the focus problem.
No, what I show is one group, Fonts. There happens to be another group,
InclTitle, so what I actually have is:

BoldOption.GroupName = "Fonts"
MatchTextOption.GroupName = "Fonts"
RegularOption.GroupName = "Fonts"

YesOption.GroupName = "InclTitle"
NoOption.GroupName = "InclTitle"

These groups work correctly as "radio buttons", so that clicking an option
unselects the previous selection in the group.

As long as you're reading, I just discovered a new issue. Let's say I open
Word by opening PubA.doc. Auto_Exec runs and creates the toolbar. Then I open
PubB.doc. When I run a function from the toolbar of PubB.doc, the focus goes
back to PubA.doc and the function acts on PubA.doc. So the toolbar seems to
apply only to the first document I opened. Does this sound familiar?
I previously wrote that I had this solved, but I don't. I've tried putting my
code in both UserForm_Initialize and UserForm_Activate. It doesn't
[quoted text clipped - 17 lines]
as a group.) How does this affect the issue of showing current settings?
Thanks for any help.
 
D

Doug Robbins - Word MVP

We would need to see the code that is being run by the control on the
toolbar.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

christophercbrewster via OfficeKB.com said:
I think I've been looking at the wrong problem. It's really the new issue I
raised. Pertaining to the original issue, I found a minor error that was
creating a problem. This combined with the document focus changing
(apparently without my noticing it) was giving me weird results. So my
real
problem is the document focus. Thanks for your patience, and would like
any
suggestions about the focus problem.
No, what I show is one group, Fonts. There happens to be another group,
InclTitle, so what I actually have is:

BoldOption.GroupName = "Fonts"
MatchTextOption.GroupName = "Fonts"
RegularOption.GroupName = "Fonts"

YesOption.GroupName = "InclTitle"
NoOption.GroupName = "InclTitle"

These groups work correctly as "radio buttons", so that clicking an option
unselects the previous selection in the group.

As long as you're reading, I just discovered a new issue. Let's say I open
Word by opening PubA.doc. Auto_Exec runs and creates the toolbar. Then I
open
PubB.doc. When I run a function from the toolbar of PubB.doc, the focus
goes
back to PubA.doc and the function acts on PubA.doc. So the toolbar seems
to
apply only to the first document I opened. Does this sound familiar?
I previously wrote that I had this solved, but I don't. I've tried
putting my
code in both UserForm_Initialize and UserForm_Activate. It doesn't
[quoted text clipped - 17 lines]
as a group.) How does this affect the issue of showing current settings?
Thanks for any help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top