Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    3 Star Lounger
    Join Date
    Jul 2002
    Location
    Edison, New Jersey, USA
    Posts
    215
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Applying/Un-Applying (removing) styles (Word 97)

    I have a macro that applies the style bold. (rather than just hitting MSO's bold button). So, now my user has a paragraph with a word that has had the style BOLD applied. Now the user decides that they want to remove the style bold and return it to the original style. How do I write something that will say:

    Hmmm... is this already Style Bold? No? Then apply Bold. Yes? Return to original formatting.

    Additionally, Can this be done by having a checkbox infront of the button, or having it toggle (like the bold button)?

  2. #2
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,853
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Applying/Un-Applying (removing) styles (Word 97)

    Have you tried
    Selection.Font.Reset
    which returns the selection to the font attributes as per the paragraph style.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  3. #3
    3 Star Lounger
    Join Date
    Jul 2002
    Location
    Edison, New Jersey, USA
    Posts
    215
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Re: Applying/Un-Applying (removing) styles (Word 97)

    so I could do something like:
    If Style Bold = True, Then Selection.Font.Reset
    else if Style Bold = False, Then Apply Style Bold
    ???

  4. #4
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,853
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Applying/Un-Applying (removing) styles (Word 97)

    I missed the bit about toggling the bold. What about using something along the lines of
    <pre>With Selection.Font
    If .Bold = True Then
    .Name = .Name & " Bold"
    .Bold = False
    ElseIf Right(.Name, 5) = " Bold" Then
    .Name = Left(.Name, Len(.Name) - 5)
    Else
    .Name = .Name & " Bold"
    End If
    End With</pre>

    It is possible to make the button appear depressed but it will add a system overhead as you will need to run a macro every time the selection changes to work out whether the current selection typeface is bold.

    You will also need a serious error checker to make sure that the bold/unbold variation of the typeface is present and named accordingly. Looking back on the cases here, you should probably do the elseif test first and then you could use an else to wrap it up.

    NOTE: I haven't tested this code but I'm guessing it might do the job you require.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  5. #5
    3 Star Lounger
    Join Date
    Jul 2002
    Location
    Edison, New Jersey, USA
    Posts
    215
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Re: Applying/Un-Applying (removing) styles (Word 97)

    Awesome!!!! IT WORKED <img src=/S/groovin.gif border=0 alt=groovin width=21 height=21>

    But I have a question..... What was the 5/ -5?

  6. #6
    3 Star Lounger
    Join Date
    Jul 2002
    Location
    Edison, New Jersey, USA
    Posts
    215
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Re: Applying/Un-Applying (removing) styles (Word 97)

    OOOOPS, Not soooo fast. I just discovered that it applys bold, but not the STYLE defined as bold.....
    I'm playing with using the theory that you did and incorporating Selection.Style = ActiveDocument.Styles("Bold") and
    Selection.Style = ActiveDocument.Styles("Default Paragraph Font")... I'm still playing with it...

  7. #7
    New Lounger
    Join Date
    Feb 2003
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Applying/Un-Applying (removing) styles (Word 97)

    I think you should be able to do this easily using the search-and-replace dialog box instead of writing a macro. You can search on any kind of style and replace it with any other kind of style, or standard Word formatting, etc. Just leave the text box blank and Word will find all text formatted with your specified style.
    If you want to remove the custom style throughout the document, just delete it in the Format/Style dialog box list.

  8. #8
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,853
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Applying/Un-Applying (removing) styles (Word 97)

    Its taking me a while to work out what it is you are after.

    Let me get this straight - You have a character style (called bold) in your template which applies bold. You want a way to toggle the selected text to use this character style or return the text to the paragraph style formats.
    If this is the case then the previous macro is worthless. If it is the case then you could try this macro instead
    <pre> If Selection.Style = ActiveDocument.Styles("bold") Then
    Selection.Font.Reset
    Else
    Selection.Style = ActiveDocument.Styles("bold")
    End If</pre>

    The previous posted macro was toggling a font change without playing with styles at all. The 5's were looking at the last 5 characters in the typeface name and checking to see whether it said " Bold". If it did then that string was removed from the typeface name. If it didn't then it was added.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  9. #9
    3 Star Lounger
    Join Date
    Jul 2002
    Location
    Edison, New Jersey, USA
    Posts
    215
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Re: Applying/Un-Applying (removing) styles (Word 97)

    Now it woks perfectly!!!!! Kudos and MANY Thanks <img src=/S/smile.gif border=0 alt=smile width=15 height=15> <img src=/S/bananas.gif border=0 alt=bananas width=33 height=35>

    (It even worked in XP)

  10. #10
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Applying/Un-Applying (removing) styles (Word 97)

    Musical,

    You can also make the built-in bold button toggle up or down while applying/removing your custom "Bold" character style as follows:

    <pre>Public Sub Bold()
    'Intercepts the built-in function
    Dim Rng As Range
    Set Rng = Selection.Range

    If Rng.Style = "BOLD" Then
    Selection.Font.Reset
    Else:
    Rng.Style = "BOLD"
    End If

    Set Rng = Nothing
    End Sub
    </pre>

    Because this intercepts Word's built-in Bold function, you get the bold buttons 'depress/undepress' functionality for free so to speak. It will also work as a toggle if the user keys Ctl+B.

    You might not want to intercept the built-in Bold function this way globally, but if you have a specific template where you need to enforce a character style for Bold rather than just Font.Bold, this is a valid thing to do.
    An example of why you might want to do this: we have a template where the font for regular text is Frutiger 55 Roman, however for bolded text what is to be used is not Frutiger 55 Roman with bold applied, rather what needs to be used then is Frutiger 45 Light with Bold applied. The above solution was the only way to do this in a way the users would accept.

    Gary

  11. #11
    3 Star Lounger
    Join Date
    Jul 2002
    Location
    Edison, New Jersey, USA
    Posts
    215
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Re: Applying/Un-Applying (removing) styles (Word 97)

    This is EXACTLY what I'm wanting, though It doesn't seem to "undo".

  12. #12
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Applying/Un-Applying (removing) styles (Word 97)

    I believe that only actions that affect a selection appear in the Undo list, but I haven't done enough experimentation to say that for sure.

    If you change the line Rng.Style = "BOLD" to Selection.Style = "BOLD" does that do what you want it to?

  13. #13
    3 Star Lounger
    Join Date
    Jul 2002
    Location
    Edison, New Jersey, USA
    Posts
    215
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Re: Applying/Un-Applying (removing) styles (Word 97)

    Nope, didn't work. [img]/forums/images/smilies/sad.gif[/img]

  14. #14
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Applying/Un-Applying (removing) styles (Word 97)

    Okay, this could be a version difference. In Word 2000, I get undo options even if I use Gary's code "as was."
    Attached Images Attached Images

  15. #15
    3 Star Lounger
    Join Date
    Jul 2002
    Location
    Edison, New Jersey, USA
    Posts
    215
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Re: Applying/Un-Applying (removing) styles (Word 97)

    OK, the following attachment has the macro, as stated above......

    But for some unknown reason, my template wont reset the text.... ?
    Attached Files Attached Files

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •