PDA

View Full Version : Change style colour with button



raspcave
11-13-2007, 09:42 AM
Hi all:

I am new to this forum, and new to VBA as well. Hope somebody can help me out.

I need to prepare some worksheets for classwork, and I want to include the solutions within the worksheet. The best way I have is to assign the solutions to a style and toggle visibility by changing its colour.

However, I need to submit these worksheets, and the receiver may not know how to do the above. Can I use a button to perform the action?

Thanks.

OTWarrior
11-13-2007, 09:51 AM
I don't follow what you mean by "Assign the solutions to a style", but I am guessing you want the text to appear when the user hits the button

the best way I have found to discover how to do something in vba is to record a macro of you doing the task, and to look at what it is doing. The downside is there tends to be alot of wasted code, but it will get you started.

Also, by worksheets do you mean documents, or Excel worksheets?

however, here is some code that may also help:

activedocument.Paragraphs(2).Range.Text = "Solution 1"

this will add the words "Solution 1" into paragrah 2

raspcave
11-13-2007, 10:04 AM
Hi OTWarrior:

Thanks for the quick reply. Let me clarify a few things first.

I am a teacher, so worksheets actually mean Word documents intended for students' assessment. I need a button that can toggle the font colour of a style (which I will assign all solutions to) between black and white. The user can then opt to print the document with or without solutions.

I tried using macros, but it can't seem to record the particular font style I am changing.

Hope I clarified your doubts.

OTWarrior
11-13-2007, 10:12 AM
so what you would need is to have a style set up for all the solutions, and search for all text with that style, and change the font colour?

Sub Macro1()
With ActiveDocument.Paragraphs(3).Range
If .Style = ActiveDocument.Styles("Heading 2") Then
If .Font.Color = wdColorWhite Then
.Font.Color = wdColorAutomatic
Else
.Font.Color = wdColorWhite
End If
End If
End With
End Sub


You would need to put this into a search parameter, but make sure it works at your end first (it works fine on mine)

lucas
11-13-2007, 03:25 PM
This is a handy one that I yoinked from Gerry some time ago.
Hide show the style no matter where it is in the document so if all of your solutions are one style...no problem.
Sub Hide()
Dim oStyle As Style
Set oStyle = ActiveDocument.Styles("Solutions")
oStyle.Font.Hidden = True
End Sub

Sub Show()
Dim oStyle As Style
Set oStyle = ActiveDocument.Styles("Solutions")
oStyle.Font.Hidden = False
End Sub

example file attached

It may be more useful to make the fonts white but it will leave a lot of blank areas in your pages....6 of 1 I guess....depends on your need.

lucas
11-13-2007, 03:39 PM
Another way to do this would be to set 2 styles....one visible and one hidden. the hidden one would have white fonts and use a toggle...

another one I yoinked from Gerry....toggle styles
Sub FlipStyles()
Dim oPara As Paragraph
For Each oPara In ActiveDocument.Paragraphs
Select Case oPara.Style
Case "hidden"
oPara.Style = "visable"
Case "visable"
oPara.Style = "hidden"
End Select
Next
End Sub

example attached

If you want to add a button after help or somewhere on the main menu to run it click here (http://slucas.virtualave.net/Wink/CustomMenuItem.htm)for a tip

raspcave
11-15-2007, 02:42 AM
Hi everyone:

Thanks for all the friendly advice! I would try them out and report on the outcome.