0

msWord: I want to change the text color of shadowed texts to red and remove the shadow.

I wrote the code below. Makes the shaded text color red. But I couldn't remove the shadow and the code doesn't stop, system crashes. Thank you for your help.

Sub Make_Highlighted_Text_Red()
    With Selection.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = ""
        .MatchWildcards = False
        .Forward = True
        .Wrap = wdFindContinue
        .Highlight = True
    Do
        .Execute
        Selection.Range.Font.ColorIndex = 6
    Loop
    End With
End Sub

8
  • Get rid of the do and loop, you need to use wdreplaceall to do the replace on every found instance.
    – freeflow
    Dec 20, 2022 at 18:57
  • I would replace with a character style of a red text rather than directly formatting. Direct formatting adds far greater complexity to a document file than does style-based formatting. Here is my writing on this with some examples: addbalance.com/usersguide/styles.htm#Overview Here is an article showing vba that creates a character style with a language attribute. answers.microsoft.com/en-us/msoffice/forum/all/… Dec 20, 2022 at 20:14
  • There is an argument for not doing that for highlighting. Bold italic and underlining can be seen as part of presenting the story, i.e. part of the layout. Highlighting has a different semantic meaning, i.e. it is extracting metadata from the document and is not part of the layout. I appreciate that historically bold, italics and underlining may have been used for the same reason as highlighting, but I hope you can appreciate the point I'm making. Its a stylistic issue which is the authors choice.
    – freeflow
    Dec 20, 2022 at 22:14
  • Thank you @Charles Kenyon . The article and your article were instructive.
    – Sitare
    Dec 21, 2022 at 10:44
  • Got it, @freeflow I should have written more clearly. My purpose for highlighting was Text Hightilght Color.
    – Sitare
    Dec 21, 2022 at 11:17

1 Answer 1

0

Thanks everyone. I solved my problem with the codes below.

      Sub No_Shadow_Font_Color()
      Dim fndColor As String
      Dim rplcColor As String
      Dim myDoc As Document
      Dim myRng As Range
      Application.ScreenUpdating = False
      Set myDoc = ActiveDocument
      fndColor = wdRed 'Attention Please write the shadow color you want to change here
      rplcColor = wdAuto
      With Selection
        .HomeKey Unit:=wdStory
        With Selection.Find
          .Highlight = True
          Do While .Execute
            If Selection.Range.HighlightColorIndex = fndColor Then
              Set myRng = Selection.Range
              myRng.HighlightColorIndex = rplcColor
              myRng.Font.ColorIndex = 6
              Selection.Collapse wdCollapseEnd
            End If
          Loop
        End With
      End With
    Application.ScreenUpdating = True
    Set myDoc = Nothing
    End Sub
3
  • fndColor and rplcColor are both incorrectly declared as String. As these variables are being used to hold colors they should be declared as Long Dec 22, 2022 at 19:20
  • Thanks for the warning. I thought of wdRed as string. I didn't realize it when I solved my problem as string.
    – Sitare
    Dec 22, 2022 at 19:43
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Dec 23, 2022 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.