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

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Applying/Un-Applying (removing) styles (Word 97)

    Author
    Topic
    #383210

    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)?

    Viewing 2 reply threads
    Author
    Replies
    • #652715

      Have you tried
      Selection.Font.Reset
      which returns the selection to the font attributes as per the paragraph style.

      • #652755

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

        • #652772

          I missed the bit about toggling the bold. What about using something along the lines of

          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

          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.

          • #652904

            Awesome!!!! IT WORKED groovin

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

            • #652905

              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…

            • #653005

              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

                If Selection.Style = ActiveDocument.Styles("bold") Then
                  Selection.Font.Reset
                Else
                  Selection.Style = ActiveDocument.Styles("bold")
                End If

              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.

            • #653066

              Now it woks perfectly!!!!! Kudos and MANY Thanks smile bananas

              (It even worked in XP)

            • #653086

              Musical,

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

              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
              

              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

            • #653257

              This is EXACTLY what I’m wanting, though It doesn’t seem to “undo”.

            • #653468

              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?

            • #653490

              Nope, didn’t work.

            • #653521

              Okay, this could be a version difference. In Word 2000, I get undo options even if I use Gary’s code “as was.”

            • #653559

              I think I’ve created some confustion…. I’m not wanting to UNDO, I’m wanting to UNAPPLY/ return to Default Paragraph Font.

            • #653494

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

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

    • #653003

      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.

    • #653616

      Can we get a bit more clarity in the requirements here, I think you are asking for this…

      1. You have a character style called bold
      2. Your character style applies bold formatting
      3. You want a macro that toggles a selection between your Bold character style and the format of the underlying paragraph style

      Is this correct?

      StuartR

      • #653671

        Yup….. thanks for saying that in english for me.

        The script that I did in my sample works fine (minus a visual toggle)…. but for some reason it doesn’t work in my working template….

        • #653766

          Can you explain the configuration of your templates and documents.
          Is the code in an “Attached Template” or a “Global Template”?
          What is the Macro called?
          Do you have a toolbar button or how do you start the Macro?

          I had a look at the word document you attached to a previous post but I couldn’t see the code at all.

          StuartR

          • #653838

            The following code is in a template module called APPLYSTYLES and looks like this:

            Public Sub Bold()
            ‘Intercepts the built-in function
            Dim Rng As Range
            Set Rng = Selection.Range
            If Selection.Style = “BOLD” Then
            Selection.Font.Reset
            Else:
            Selection.Style = “BOLD”
            End If
            Set Rng = Nothing
            End Sub

            I then have a button on the toolbar with a letter “B”.

            • #653854

              Other than the : after the else that looks right. I can’t imagine why it wouldn’t work.
              Another quick check. Are you saying that this macro correctly sets the Bold style but doesn’t clear it again.
              Can you check that Bold is a character style, and not a paragraph style. (This is correct in the sample document you posted).
              Can you try selecting a bold word and typing Control-Space to see if it resets.

              StuartR

            • #654068

              The ranges are doing nothing for you in the example code you posted. I would also question the Else:

              I also think you need to define where the styles are coming from if you want the global template to function correctly. Going back to the code that you did say worked – can you test this again?

              Public Sub Bold()
              'Intercepts the built-in function
                If Selection.Style = ActiveDocument.Styles("BOLD") Then
                  Selection.Font.Reset
                Else
                  Selection.Style = ActiveDocument.Styles("BOLD")
                End If
              End Sub
            • #654101

              Per Stuart’s question in previous post: have you placed this in an attached template, or a global template? The example I posted was designed to be used in an attached template, I can’t vouch for whether it would work correctly in a global template.

              Gary

    Viewing 2 reply threads
    Reply To: Reply #653766 in Applying/Un-Applying (removing) styles (Word 97)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information:




    Cancel