PDA

View Full Version : Change font in Text Boxes Macro



lady_miss22
10-28-2008, 10:32 PM
How can I manipulate this code so that it also reads through text boxes? It's currently an on/off macro written to find bolded text and turn it white essentially to make it "disappear" and vice versa. There is also word art that appears and disappears (this is a teachers quiz) However it skips through the text boxes.

Sub ShowAnswers()
Dim Flag As Boolean
Dim aShape As Shape
Dim sLine As String
Dim sTrans As String
Dim sSource As Document
Set sSource = ActiveDocument
Flag = False
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
Selection.Find.Font.Color = wdColorWhite
With Selection.Find
If .Execute = True Then
Flag = True
End If
Do While .Execute(FindText:="", MatchWildcards:=False, MatchCase:=True, Wrap:=wdFindContinue, Forward:=True) = True
With Selection.Font
.Color = wdColorAutomatic
.Bold = True
End With
Loop
End With
If Flag = True Then
sTrans = 1#
sLine = msoFalse
With sSource
For Each aShape In .Shapes
If aShape.Type = msoTextEffect Then
aShape.Select
With Selection
.ShapeRange.Fill.Transparency = sTrans
.ShapeRange.Line.Visible = sLine
End With
End If
Next aShape
End With
Exit Sub
Else
sTrans = 0#
sLine = msoTrue
End If
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
Selection.Find.Font.Color = wdColorAutomatic
Selection.Find.Font.Bold = True
With Selection.Find
Do While .Execute(FindText:="", MatchWildcards:=False, MatchCase:=True, Wrap:=wdFindContinue, Forward:=True) = True
With Selection.Font
.Color = wdColorWhite
End With
Loop
End With
With sSource
For Each aShape In .Shapes
If aShape.Type = msoTextEffect Then
aShape.Select
With Selection
.ShapeRange.Fill.Transparency = sTrans
.ShapeRange.Line.Visible = sLine
End With
End If
Next aShape
End With
End Sub


Also, I found the following code on how to do it and would much rather use this one instead of the selection.find but I have no idea how to make it work like an on/off macro and do what my first code does.

Public Sub FindReplaceAlmostAnywhere()

Dim rngStory As Word.Range
Dim lngJunk As Long

'Fix the skipped blank Header/Footer problem as provided by Peter Hewett
lngJunk = ActiveDocument.Sections( 1 ).Headers( 1 ).Range.StoryType
'Iterate through all story types in the current document
For Each rngStory In ActiveDocument.StoryRanges
'Iterate through all linked stories
Do
With rngStory.Find
.Text = "find text"
.Replacement.Text = "I'm found"
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
'Get next linked story (if any)
Set rngStory = rngStory.NextStoryRange
Loop Until rngStory Is Nothing
Next

End Sub


PLEASE HELP!

Edit by lucas: VBA tags added to code. lady_miss22, if you select your code when posting and hit the vba button your code will be formatted as above.

lucas
10-29-2008, 02:41 PM
I didn't look too closley at your code because I think I would use styles to deal with your problem, if I understand correctly.

The following will change or toggle text from one style "visible" to another style "hidden" both in each paragraph of the document and in the textboxes.

See attached example....use the show/hide menu next to help on the main menu.

Option Explicit
Sub FlipStyles()
Dim oPara As Paragraph
For Each oPara In ActiveDocument.Paragraphs
Select Case oPara.Style
Case "hidden"
oPara.Style = "visible"
Case "visible"
oPara.Style = "hidden"
End Select
Next
Dim eShape As Shape
Dim aDoc As Document
Set aDoc = ActiveDocument
For Each eShape In aDoc.Shapes
If eShape.Type = msoTextBox Then
With eShape
With .TextFrame.TextRange
Select Case eShape.TextFrame.TextRange.Style
Case "hidden"
eShape.TextFrame.TextRange.Style = "visible"
Case "visible"
eShape.TextFrame.TextRange.Style = "hidden"
End Select
End With
End With
End If
Next
End Sub

lady_miss22
10-29-2008, 06:35 PM
Thats awesome! So how do I get it to selectively show/hide certain words? In other words what makes the answer disappear and reappear?

I have lines underneath the answers and had tried the Hiddentext method but the lines moved around the page once the words went hidden.

I've attached a sample of a few pages of the quiz

lucas
10-29-2008, 07:24 PM
look at my document again and go to the main menu.....format/ styles and formatting.

You will see that the style of the text to hide is either style visible(if you can see it) or hidden(if you can't see it). Same thing you were doing with white fonts except using styles to do it.


Any word you want to dissappear just select it and assign the style "visible" to it.

lady_miss22
10-29-2008, 07:53 PM
Thanks Again Lucas :)

I don't know how that would work with my situation since the answers are in different styles, fonts, sizes..some are in tables..others in text boxes..some double spaced..others singles..Does that means I would have to add a hidden/visible style for each answer ? The final document is over 50 pages long.. :/

lucas
10-29-2008, 08:01 PM
Well, my question would be why would you have many different styles? You should be using style for conformity in the document I would think.

When I opened your document it indicated to me that one of your tables is corrupt anyway.

I would seriously consider redesigning the layout but maybe someone else will come along with a really creative idea. Word is not my strong suit. I'm really just an AutoCAD guy who hangs out here.

With styles you can get away from using shapes to underline your text also......

lady_miss22
10-29-2008, 08:10 PM
That is what I say exactly!! Mostly because some answers are in the heading..some in smaller font in tables and so on...I didn't design this, the teacher gave me the rought draft and asked me to clean it up and create that on/off macro :P

Most of it doesnt' make sense to me BUT I LOVE your idea.. it's much simpler!

Trying to figure out how to incorporate it :D I really appreciate your help :content:

lucas
10-29-2008, 08:28 PM
maybe you could start with my sample and change the visible and hidden styles to something you can use. I would avoid the textboxes but that is my preference.

fumei
10-30-2008, 10:14 AM
" I would avoid the textboxes but that is my preference."

LOL

lucas
10-30-2008, 11:43 AM
Hey Gerry, you rascal. You know I feel like I'm lost and alone in the forrest with Word.......

On a serious note Gerry, why does the code not work well on ladymiss's attachment.

Could you give some clarification for those of us who don't understand the creation and life of textboxes? There seems to be more than one type and I also don't understand the frames that surround them or thier use.

fumei
10-30-2008, 01:49 PM
OMG....I had not looked at the document previously.

AI CARAMBA!!!!!!!!!!!

1. Option Explicit. Option Explicit. Option Explicit.
2. I hate graphic textboxes. I hate graphic textboxes. I hate graphic textboxes.

Why? Because of just the issue you mention. There are indeed different types, and working with the frames is simply a nightmare. I would be more than happy if Word just got rid of them and never allowed another graphical textbox again.

I would absolutely re-design this.

First of all, I would find that "corrupted table" and get rid of it....which I did.

Second, it is not just poor design, it is BAD design to have different font size for similar structure. The "answers" inside the boxes should be the same font size. Some are 18, some are 20.

This document is really ugly and messed up. I do not AT ALL mean to be offensive, in any way, when I say that. My apologies, but it simply just is...ugly and messed up.

The lack of proper Styles is its greatest flaw.

I am attaching a demo. Go to, say, the "Automated Developer Tests" page, and click "Gerry Toggle Answers" on the top toolbar.

Voila! The answers vanish from the boxes. Click it again...they re-appear. The WHOLE code to do this is:

Sub ToggleAnswers()
Dim oPara As Paragraph
For Each oPara In ActiveDocument.Paragraphs
If oPara.Style = "Ans_Vis" Then
oPara.Style = "Ans_Invis"
ElseIf oPara.Style = "Ans_Invis" Then
oPara.Style = "Ans_Vis"
End If
Next
End Sub

It goes through each paragraph.

If the paragraph has the Style "Ans_Vis", it makes it "Ans_Invis".

If it is "Ans_Invis", it makes it "Ans_Vis".

Done.

I did not go through all of the document and fix things, as...why? It needs a full re-design. It would take me hours to really fix it, and I am not going to do that. I did remove those awful red drawing lines...simply because I could not stand them. I also made the gigantic heading format its own style ("BigHeading")...and used it. Then I discovered that the first page was a mess of conflicting format structures used to make placement of the grapgic boxes.

Realizing that I would indeed have to take some serious time to fix this...I did not.

Frankly, I would get rid of ALL of the drawing objects. If the first page has to have those arrowing things (bleeech), and it is going to be permanent - it has no answers, so there is nothing toggled - then I would do it as a proper graphic and insert it as ONE graphic image.

As for the Answers being in those upside down graphic boxes....sigh.....

First off, what is the point of making the answer disappear, then make them APPEAR (although upside down) on the same page. Seems...odd.

You can make them toggle in visible by (and I am not going to supply the code, although I did write it and test it):

1. bookmark each answer object
2. toggle the object.Line.Visible

' pseudo-code

If object.Line.Visible = True Then
object.Line.Visible = False
Else
object.Line.Visible = True
End

and that is it. The text answers can be toggled visible/invisible using proper styles. The textbox graphic answers can be toggled visible/invisible by Line.Visible

Note: the attached file does NOT have the code for toggling the graphic boxes via bookmarks. I did that in the original file.