Consulting

Results 1 to 5 of 5

Thread: Search and replace in table

  1. #1
    VBAX Newbie
    Joined
    Dec 2020
    Posts
    3
    Location

    Search and replace in table

    Hello all

    I have multiple word documents with a lot of tables inside each.
    All the tables have the same format.

    The first line of the table is always grey background.

    I need create a vba macro who can search and replace the background color of the first ligne table, change the color of the tables border and change font color of text when backgound is grey.

    I have tried something with word styles but it is not optimal,
    Please look test here (macro named TABLEAU)
    https://we.tl/b-BXxwZXOZGS

    Thanks a lot for help

    Tom

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    Sub YourMatchMacro()
      ScratchMacro ActiveDocument
    End Sub
    
    Sub ScratchMacro(oDoc)
    'A basic Word macro coded by Greg Maxey
    Dim oTbl As Table
      Set oDoc = ActiveDocument
      For Each oTbl In ActiveDocument.Tables
        With oTbl
          If .Rows(1).Range.Shading.BackgroundPatternColorIndex = wdGray25 Then
            .Rows(1).Range.Shading.BackgroundPatternColorIndex = wdWhite
            .Rows(1).Range.Font.ColorIndexBi = wdBrightGreen
            .Rows(1).Range.Shading.BackgroundPatternColorIndex = wdGray25
            .Borders.OutsideColor = wdColorBrightGreen
            .Borders.InsideColor = wdColorGold
          End If
        End With
      Next
    lbl_Exit:
      Exit Sub
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Newbie
    Joined
    Dec 2020
    Posts
    3
    Location
    Quote Originally Posted by gmaxey View Post
    Sub YourMatchMacro()
      ScratchMacro ActiveDocument
    End Sub
    
    Sub ScratchMacro(oDoc)
    'A basic Word macro coded by Greg Maxey
    Dim oTbl As Table
      Set oDoc = ActiveDocument
      For Each oTbl In ActiveDocument.Tables
        With oTbl
          If .Rows(1).Range.Shading.BackgroundPatternColorIndex = wdGray25 Then
            .Rows(1).Range.Shading.BackgroundPatternColorIndex = wdWhite
            .Rows(1).Range.Font.ColorIndexBi = wdBrightGreen
            .Rows(1).Range.Shading.BackgroundPatternColorIndex = wdGray25
            .Borders.OutsideColor = wdColorBrightGreen
            .Borders.InsideColor = wdColorGold
          End If
        End With
      Next
    lbl_Exit:
      Exit Sub
    End Sub
    Thanks a lot Greg,
    it's really very very friendly to have written this code to help me.
    it's really very pleasant because tonight I'm very bored, locked up at home because of the Coronavirus.

    I have tried, modify a little bit your code for testing,
    You can see it here :

    https://we.tl/b-x17c3pgecS

    Could you help me more for adjust it?
    1 - The following line do not apply any change
    Borders.InsideColor = wdColorGold
    (I want apply a color at all borders of the tables if
    .Rows(1).Range.Shading.BackgroundPatternColorIndex = wdGray25 Then)
    I need adjust size of all border too

    2 - If you look at the first table on the document you'll can see that the first rows is divided into three cells.
    Then the code is not applied at this table.
    The best will be to search grey background cell and apply code.

    I hope i explain it clearly

    Thanks a lot and very good new year, i hope better than 2020.

    Thomas

  4. #4
    VBAX Newbie
    Joined
    Dec 2020
    Posts
    3
    Location
    Help please
    Can i ask a little up?

    Thanks all and happy new year to all

  5. #5
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by Tom0ne View Post

    I have tried, modify a little bit your code for testing,
    You can see it here :

    https://we.tl/b-x17c3pgecS

    Could you help me more for adjust it?
    Kindly attach any documents etc you want people to view to your post here instead of loading on third-party sites where people have to sign up to access your files.

    You can attach files via 'Go Advanced' at the bottom of the posting/reply screen, then selecting the paper-clip symbol.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Posting Permissions

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