Results 1 to 9 of 9
  1. #1
    4 Star Lounger
    Join Date
    Aug 2003
    Location
    Stroud, United Kingdom
    Posts
    548
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Find Super/Sub script (XP)

    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 [img]/forums/images/smilies/sad.gif[/img]

    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
    Thanks,

    pmatz

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Find Super/Sub script (XP)

    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".

  3. #3
    4 Star Lounger
    Join Date
    Aug 2003
    Location
    Stroud, United Kingdom
    Posts
    548
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Find Super/Sub script (XP)

    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...
    Thanks,

    pmatz

  4. #4
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Find Super/Sub script (XP)

    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:

    <pre>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
    </pre>


    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:

    <pre>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
    </pre>


    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

  5. #5
    4 Star Lounger
    Join Date
    Aug 2003
    Location
    Stroud, United Kingdom
    Posts
    548
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Find Super/Sub script (XP)

    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 [img]/forums/images/smilies/smile.gif[/img]
    Thanks,

    pmatz

  6. #6
    4 Star Lounger
    Join Date
    Aug 2003
    Location
    Stroud, United Kingdom
    Posts
    548
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Find Super/Sub script (XP)

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

    <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

    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
    Thanks,

    pmatz

  7. #7
    4 Star Lounger
    Join Date
    Aug 2003
    Location
    Stroud, United Kingdom
    Posts
    548
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Find Super/Sub script (XP)

    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

    <img src=/S/coffeetime.gif border=0 alt=coffeetime width=32 height=48>
    Thanks,

    pmatz

  8. #8
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Find Super/Sub script (XP)

    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

  9. #9
    4 Star Lounger
    Join Date
    Aug 2003
    Location
    Stroud, United Kingdom
    Posts
    548
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Find Super/Sub script (XP)

    Thanks Stuart, I like that! <img src=/S/yep.gif border=0 alt=yep width=15 height=15>

    My method is pretty fast, so I will keep it for now, but the macro you shown is clever. Food for thought for me <img src=/S/smile.gif border=0 alt=smile width=15 height=15>
    Thanks,

    pmatz

Posting Permissions

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