Consulting

Page 2 of 2 FirstFirst 1 2
Results 21 to 28 of 28

Thread: Macro cannot find shading colour black

  1. #21
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by Aquinax View Post
    The colour 'black' is found when searched for 'yellow' by you, because the search for black is too generic inside the Word's internal coding
    Not so. There is a bug in the interaction with Find and BackgroundPatternColorIndex/BackgroundPatternColor. A Find for wdBlack Finds everything that doesn't have a Background Color. A Find for wdYellow Finds both Yellow and Black. The workaround is do a Find for wdYellow and to apply an additional test to determine which of Yellow and Black it is. That's what the code I posted does.
    Last edited by macropod; 05-15-2018 at 04:23 PM.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  2. #22
    giphy (1).jpg

    By me your macro in its original form doesn't search at all. Only if I add Selection to With .Find and cut out the first lines like this:
    Sub Demo()  
      With Selection.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "*^13"
        .Replacement.Text = ""
        .ParagraphFormat.Shading.BackgroundPatternColorIndex = wdYellow
        .Format = True
        .Forward = True
        .MatchWildcards = True
        .Wrap = wdFindStop
        .Execute
      End With
    
    End Sub
    does it start to search and indeed it finds yellow and skips black; and also finds other colours. It is the "No colour" shading fill which is the problem for finding black shaded par-s.

  3. #23
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by Aquinax View Post
    By me your macro in its original form doesn't search at all. Only if I add Selection to With .Find and cut out the first lines like this:
    The code I posted works fine; it's your poor attempt to implement a revision of it that's the problem...
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  4. #24
    Certainly, it works fine by you, at your machine and system with their own specifics. What is true is that it doesn't work by me and that "searching yellow finds black and it's a bug" is untrue, as searching yellow finds yellow and thus there's no bug, and what is also true is that searching black finds "No colour" as well, and this is the problem for me

  5. #25
    VBAX Tutor
    Joined
    Jul 2016
    Posts
    266
    Location
    This works for me:

    Sub DeleteBlackShadedParagraphs()
    Application.ScreenUpdating = False
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "*^13"
        .Replacement.Text = ""
        .ParagraphFormat.Shading.BackgroundPatternColor = -587137025
        .Format = True
        .Forward = True
        .MatchWildcards = True
        .Wrap = wdFindStop
        .Execute
      End With
      Do While .Find.Found
        If .Shading.BackgroundPatternColor = -587137025 Then .Delete
        .Collapse wdCollapseEnd
        .Find.Execute
      Loop
    End With
    Application.ScreenUpdating = True
    End Sub

  6. #26
    VBAX Tutor
    Joined
    Jul 2016
    Posts
    266
    Location
    It doesn't seem to work on your sample document. I'm curious what method you're using to shade the paragraphs? When I choose shade "Black, Text 1" it works fine.

  7. #27
    VBAX Tutor
    Joined
    Jul 2016
    Posts
    266
    Location
    This works on your document:

    Sub ChangeShadingAndThenDelete()
    Application.ScreenUpdating = False
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "*^13"
        .Replacement.Text = ""
        .ParagraphFormat.Shading.BackgroundPatternColor = RGB(0, 0, 0)
        .Format = True
        .Forward = True
        .MatchWildcards = True
        .Wrap = wdFindStop
        .Execute
      End With
      Do While .Find.Found
        If .Shading.BackgroundPatternColor = RGB(0, 0, 0) Then .Shading.BackgroundPatternColor = -587137025
        .Collapse wdCollapseEnd
        .Find.Execute
      Loop
    End With
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "*^13"
        .Replacement.Text = ""
        .ParagraphFormat.Shading.BackgroundPatternColor = -587137025
        .Format = True
        .Forward = True
        .MatchWildcards = True
        .Wrap = wdFindStop
        .Execute
      End With
      Do While .Find.Found
        If .Shading.BackgroundPatternColor = -587137025 Then .Delete
        .Collapse wdCollapseEnd
        .Find.Execute
      Loop
    End With
    Application.ScreenUpdating = True
    End Sub

  8. #28
    VBAX Tutor
    Joined
    Jul 2016
    Posts
    266
    Location
    How did you apply the shading?

Posting Permissions

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