Results 1 to 3 of 3
  1. #1
    New Lounger
    Join Date
    Feb 2014
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Select Paragraphs in a Table and Change to Red

    I am trying to change certain paragraphs in a table cell to red if they have the prefix "RED:"

    The code below finds all of the paragraphs (including empty paragraphs) - but it turns ALL of the paragraphs red instead of just the ones that start with "RED:"

    What am I doing wrong????


    Code:
        Dim r As Range
        Dim aCell As Cell
        Dim myTable As Table
        
    
    
     For Each aCell In myTable.Columns(2).Cells
        Set r = aCell.Range
        For Each Paragraph In r.Paragraphs
             Selection.Find.ClearFormatting
             Selection.Find.Replacement.ClearFormatting
             With Selection.Find
                .Text = "RED:"
                .Format = True
                .Forward = True
                .Wrap = wdFindStop
                .MatchCase = False
                .MatchWholeWord = False
                .MatchWildcards = Fales
                .MatchSoundsLike = False
                .MatchAllWordForms = False
            End With
            
            If Selection.Find.Found Then
                Selection.StartOf Unit:=wdParagraph
                Selection.MoveEnd Unit:=wdParagraph
                Selection.Font.Color = wdColorRed
            End If
            Selection.Collapse wdCollapseEnd
        Next
       Next

  2. Subscribe to our Windows Secrets Newsletter - It's Free!

    Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

  3. #2
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    3,816
    Thanks
    0
    Thanked 166 Times in 153 Posts
    Try:
    Code:
    Sub Demo()
    Dim Rng As Range, aCell As Cell, myTable As Table, Para As Paragraph
    Set myTable = ActiveDocument.Tables(1)
    For Each aCell In myTable.Columns(2).Cells
      Set Rng = aCell.Range
      For Each Para In aCell.Range.Paragraphs
        With Para.Range
          With .Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Text = "RED:"
            .Format = True
            .Forward = True
            .Wrap = wdFindStop
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            .Execute
          End With
          Do While .Find.Found = True
            With .Duplicate
              If .InRange(Rng) Then
                .Paragraphs.First.Range.Font.Color = wdColorRed
              End If
            End With
            .Collapse wdCollapseEnd
            .Find.Execute
          Loop
        End With
      Next
    Next
    End Sub
    FWIW, I'd be surprised if the code you posted would do anything - it has some obvious typos (e.g. 'Fales') and no .Execute statement.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  4. #3
    New Lounger
    Join Date
    Feb 2014
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Works great! Thanks!

Posting Permissions

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