• Find Super/Sub script (XP)

    Author
    Topic
    #405428

    Hi,

    I have been doing a Find for any superscipt or subscript characters, and have been using the Find Dialog box, then clicking super/sub script in the Font properties of the box. I wanted to automate this into a macro I had and so recorded my activity as a macro. It didn’t seem to record the super/sub script bit though

    I have used this VBA as an alterrnative, which others may find useful, unless there is a better way using the Find method that I can’t ‘find’!

    ‘check for superscript / subscript characters and if present show warning
    Dim i As Integer, blnF As Boolean
    For i = 1 To ActiveDocument.Characters.Count
    If ActiveDocument.Characters(i).Font.Subscript = True Or _
    ActiveDocument.Characters(i).Font.Superscript = True Then
    blnF = True
    End If
    Next i
    If blnF Then MsgBox “There are superscript or subscript characters ” & _
    “present in this document!” , vbInformation, _
    “Reformat”

    Cheers

    Viewing 3 reply threads
    Author
    Replies
    • #832841

      For some reason, finding font formatting is not recorded in a macro, but you can write the necessary code yourself:

      With Selection.Find
      .ClearFormatting
      .Format = True
      .Font.SuperScript = True

      .Execute
      End With

      You must create separate searches for Superscript and for Subscript, the Find object does not support “find superscript or subscript”.

      • #832845

        Aha! Thank u Hans. That makes sense (the code – not why the macro doesn’t record it!)

        And it will be quicker than my VBA as I found it takes agaes on long documents!

        Cheers…

      • #832846

        Aha! Thank u Hans. That makes sense (the code – not why the macro doesn’t record it!)

        And it will be quicker than my VBA as I found it takes agaes on long documents!

        Cheers…

    • #832842

      For some reason, finding font formatting is not recorded in a macro, but you can write the necessary code yourself:

      With Selection.Find
      .ClearFormatting
      .Format = True
      .Font.SuperScript = True

      .Execute
      End With

      You must create separate searches for Superscript and for Subscript, the Find object does not support “find superscript or subscript”.

    • #833240

      Hans has pointed you in the right direction on this one, but as a sidebar, there are times when your initial approach may be useful or even necessary. When that happens, there’s a few things you can do to speed up your code.

      For example, when using Find/Replace, you can search for Highlighting, but you can’t specify which color. So if you’ve got a document that uses red and blue highlighting, and you only want to clear the red highlighting, you’re in a bind. It can be done using the Find object, but it’s much easier to code (and understand) if you use a simple For…Each loop, which is is much faster than a regular For…Next loop.

      To solve our hypothetical highlighting problem:

      Sub RemoveOnlyRedHighlighting()
      Dim char As Range
      For Each char In ActiveDocument.Characters
          If char.HighlightColorIndex = wdRed Then
              char.HighlightColorIndex = wdNoHighlight
          End If
      Next char
      End Sub
      

      In general, use a For…Each loop when you’ll be visiting each object in a collection of objects, as long as you’re not deleting any of them.

      This wouldn’t be as fast as using the Find object, but its performance is respectable, considering it hits each character. And it’s got the added benefit of being simple to code — a similar macro using the Find object would be about three times as long.

      To see how much faster a For…Each loop is, try this version of your original code:

      Sub AnySuperOrSubScripts()
      Dim char As Range
      For Each char In ActiveDocument.Characters
          If (char.Font.Subscript) Or _
              (char.Font.Superscript) Then
              MsgBox "There are superscript or subscript characters " & _
                     "present in this document!", vbInformation, _
                     "Reformat"
              Exit For
          End If
      Next char
      End Sub
      

      Since you just wanted to know if there were any sub/super script, this version stops as soon as it finds any.
      BTW, just like the Find object, this method won’t catch items in the Header/Footer, or in Footnotes, Comments, etc.

      Just my $.02

      • #833295

        Cheers Andrew77, thats cool. I did do a for each loop before, but didnt know what to declare the character variable as, so used a variant, ie:

        Dim char As Variant
        For Each char In ActiveDocument.Characters…

        and this was quite slow.

        Very helpful

        • #836013

          Im back on this … I have modified my code and some of the MS Word Examples code to give me a method of identifying how many superscript and subscript characters there are in my document using the FIND method. I am checking first for super, then for sub. It works for the first check, but then the FIND doesn’t reset to pick up the second set. If I comment out the first check then the FIND picks up the second.

          I think its to do with the ActiveDocument.Content.Find part, I have tried a few things, but don’t know how to tell FIND to start again from the beginning! Any ideas? Should I ‘End With’ each section, or is there another way I’m missing!?

          smile

          See code below: (the comment is where i imagine i do this resetting thing…)

          Dim intSub As Integer, intSuper As Integer

          With ActiveDocument.Content.Find
          .ClearFormatting
          .MatchWildcards = True
          .Font.Superscript = True
          Do While .Execute(“?”, , , , , , True) = True
          intSuper = intSuper + 1
          Loop

          ActiveDocument.Content.MoveStart ‘this doesnt work, what can i put here??
          .ClearFormatting
          .MatchWildcards = True
          .Font.Subscript = True
          Do While .Execute(“?”, , , , , , True) = True
          intSub = intSub + 1
          Loop
          End With

          If intSuper > 0 Or intSub > 0 Then
          MsgBox “There are superscript or subscript characters ” & _
          “present in this document!” & vbNewLine & _
          “Superscript” & vbTab & intSuper & vbNewLine & _
          “Subscript ” & vbTab & intSub & vbNewLine & _
          “Make sure you reformat when in template.” _
          , vbInformation, “Reformat”
          End If

          • #836027

            I have done this for now which works well and seems ‘neat code’

            Dim intSub As Integer, intSuper As Integer

            With ActiveDocument.Content.Find
            .ClearFormatting
            .MatchWildcards = True
            .Font.Superscript = True ‘check for superscript
            Do While .Execute(FindText:=”?”, Forward:=True) = True
            intSuper = intSuper + 1
            Loop
            End With

            With ActiveDocument.Content.Find
            .ClearFormatting
            .MatchWildcards = True
            .Font.Subscript = True ‘check for subscript
            Do While .Execute(FindText:=”?”, Forward:=True) = True
            intSub = intSub + 1
            Loop
            End With

            coffeetime

          • #836028

            I have done this for now which works well and seems ‘neat code’

            Dim intSub As Integer, intSuper As Integer

            With ActiveDocument.Content.Find
            .ClearFormatting
            .MatchWildcards = True
            .Font.Superscript = True ‘check for superscript
            Do While .Execute(FindText:=”?”, Forward:=True) = True
            intSuper = intSuper + 1
            Loop
            End With

            With ActiveDocument.Content.Find
            .ClearFormatting
            .MatchWildcards = True
            .Font.Subscript = True ‘check for subscript
            Do While .Execute(FindText:=”?”, Forward:=True) = True
            intSub = intSub + 1
            Loop
            End With

            coffeetime

            • #836035

              You could also do this with a variant of the CountWord macro on the MVPS web site (look about half way down the page).

              StuartR

            • #836037

              Thanks Stuart, I like that! yep

              My method is pretty fast, so I will keep it for now, but the macro you shown is clever. Food for thought for me smile

            • #836038

              Thanks Stuart, I like that! yep

              My method is pretty fast, so I will keep it for now, but the macro you shown is clever. Food for thought for me smile

            • #836036

              You could also do this with a variant of the CountWord macro on the MVPS web site (look about half way down the page).

              StuartR

        • #836014

          Im back on this … I have modified my code and some of the MS Word Examples code to give me a method of identifying how many superscript and subscript characters there are in my document using the FIND method. I am checking first for super, then for sub. It works for the first check, but then the FIND doesn’t reset to pick up the second set. If I comment out the first check then the FIND picks up the second.

          I think its to do with the ActiveDocument.Content.Find part, I have tried a few things, but don’t know how to tell FIND to start again from the beginning! Any ideas? Should I ‘End With’ each section, or is there another way I’m missing!?

          smile

          See code below: (the comment is where i imagine i do this resetting thing…)

          Dim intSub As Integer, intSuper As Integer

          With ActiveDocument.Content.Find
          .ClearFormatting
          .MatchWildcards = True
          .Font.Superscript = True
          Do While .Execute(“?”, , , , , , True) = True
          intSuper = intSuper + 1
          Loop

          ActiveDocument.Content.MoveStart ‘this doesnt work, what can i put here??
          .ClearFormatting
          .MatchWildcards = True
          .Font.Subscript = True
          Do While .Execute(“?”, , , , , , True) = True
          intSub = intSub + 1
          Loop
          End With

          If intSuper > 0 Or intSub > 0 Then
          MsgBox “There are superscript or subscript characters ” & _
          “present in this document!” & vbNewLine & _
          “Superscript” & vbTab & intSuper & vbNewLine & _
          “Subscript ” & vbTab & intSub & vbNewLine & _
          “Make sure you reformat when in template.” _
          , vbInformation, “Reformat”
          End If

      • #833296

        Cheers Andrew77, thats cool. I did do a for each loop before, but didnt know what to declare the character variable as, so used a variant, ie:

        Dim char As Variant
        For Each char In ActiveDocument.Characters…

        and this was quite slow.

        Very helpful

    • #833241

      Hans has pointed you in the right direction on this one, but as a sidebar, there are times when your initial approach may be useful or even necessary. When that happens, there’s a few things you can do to speed up your code.

      For example, when using Find/Replace, you can search for Highlighting, but you can’t specify which color. So if you’ve got a document that uses red and blue highlighting, and you only want to clear the red highlighting, you’re in a bind. It can be done using the Find object, but it’s much easier to code (and understand) if you use a simple For…Each loop, which is is much faster than a regular For…Next loop.

      To solve our hypothetical highlighting problem:

      Sub RemoveOnlyRedHighlighting()
      Dim char As Range
      For Each char In ActiveDocument.Characters
          If char.HighlightColorIndex = wdRed Then
              char.HighlightColorIndex = wdNoHighlight
          End If
      Next char
      End Sub
      

      In general, use a For…Each loop when you’ll be visiting each object in a collection of objects, as long as you’re not deleting any of them.

      This wouldn’t be as fast as using the Find object, but its performance is respectable, considering it hits each character. And it’s got the added benefit of being simple to code — a similar macro using the Find object would be about three times as long.

      To see how much faster a For…Each loop is, try this version of your original code:

      Sub AnySuperOrSubScripts()
      Dim char As Range
      For Each char In ActiveDocument.Characters
          If (char.Font.Subscript) Or _
              (char.Font.Superscript) Then
              MsgBox "There are superscript or subscript characters " & _
                     "present in this document!", vbInformation, _
                     "Reformat"
              Exit For
          End If
      Next char
      End Sub
      

      Since you just wanted to know if there were any sub/super script, this version stops as soon as it finds any.
      BTW, just like the Find object, this method won’t catch items in the Header/Footer, or in Footnotes, Comments, etc.

      Just my $.02

    Viewing 3 reply threads
    Reply To: Find Super/Sub script (XP)

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

    Your information: