none
macro to highlight and count subscript RRS feed

  • Question

  • Hi

    Is there any macro to find and highlights all subscript text and throws a message box for the count of highlights in a word document?

    I need it replace the subscript with text highlights color not font highlights color.

    John

    Saturday, June 21, 2014 4:40 AM

Answers

  • You can get a count with:

    Sub Demo()
    Application.ScreenUpdating = False
    Dim i As Long
    With ActiveDocument.Range
      With .Find
         .ClearFormatting
         .Replacement.ClearFormatting
         .Text = ""
         .Replacement.Text = ""
         .Forward = True
         .Wrap = wdFindStop
         .Format = True
         .Font.Subscript = True
         .MatchWildcards = False
        .Execute
      End With
      Do While .Find.Found
        i = i + 1
        .Collapse wdCollapseEnd
        .Find.Execute
      Loop
    End With
    Application.ScreenUpdating = True
    MsgBox i & " instances found."
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Saturday, June 21, 2014 8:23 AM
  • That's quite simple. After:

     i = i + 1
    insert:
    .HighlightColorIndex = wdYellow

    Instead of wdYellow you can use any other highlight constant you prefer.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, June 26, 2014 10:16 PM

All replies

  • Why do you need a count? Highlighting the text requires nothing more than an ordinary Find/Replace - a macro isn't even required. Still, to do it with a macro you could use:

    Sub Demo()
    Application.ScreenUpdating = False
    With ActiveDocument.Range.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .Font.Subscript = True
        .Replacement.Highlight = True
        .MatchWildcards = False
        .Execute Replace:=wdReplaceAll
    End With
    Application.ScreenUpdating = True
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Saturday, June 21, 2014 4:53 AM
  • Hi Paul,

    Thanks for your swift reply.

    Actually I want to highlight and count all the subscript text through a macro.

    Or just count the highlight text through a macro.

    John

    Saturday, June 21, 2014 6:25 AM
  • You can get a count with:

    Sub Demo()
    Application.ScreenUpdating = False
    Dim i As Long
    With ActiveDocument.Range
      With .Find
         .ClearFormatting
         .Replacement.ClearFormatting
         .Text = ""
         .Replacement.Text = ""
         .Forward = True
         .Wrap = wdFindStop
         .Format = True
         .Font.Subscript = True
         .MatchWildcards = False
        .Execute
      End With
      Do While .Find.Found
        i = i + 1
        .Collapse wdCollapseEnd
        .Find.Execute
      Loop
    End With
    Application.ScreenUpdating = True
    MsgBox i & " instances found."
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Saturday, June 21, 2014 8:23 AM
  • Hi Paul,

    Regret for the delay in reply.

    The macro works fine.

    The only thing is, I request the macro need to highlight and count.

    Now, It correctly find and count the number of subscript.

    Is there any possibility to highlight the subscript as well?

    Regard,

    John

    Thursday, June 26, 2014 7:29 PM
  • That's quite simple. After:

     i = i + 1
    insert:
    .HighlightColorIndex = wdYellow

    Instead of wdYellow you can use any other highlight constant you prefer.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, June 26, 2014 10:16 PM
  • Hi Paul,

    Thank you very much, Its works great.

    John

    Friday, June 27, 2014 11:00 PM