+ Post New Thread
Results 1 to 5 of 5
Coding Thread, VB code to colour cells in a word table in Coding and Web Development; Hello All, Do you think it would be possible (and relatively simple) to create a Macro using VB that looks ...
  1. #1

    Join Date
    Nov 2006
    Location
    Lancashire
    Posts
    95
    Thank Post
    34
    Thanked 1 Time in 1 Post
    Rep Power
    0

    VB code to colour cells in a word table

    Hello All,

    Do you think it would be possible (and relatively simple) to create a Macro using VB that looks through a table (or tables) in a word document and everywhere it finds a symbol, it gives the cell a colour?

    For example, every cell with a '@' gets coloured red.

    Thanks in advance everyone.

    Ben

  2. #2

    EduTech's Avatar
    Join Date
    Aug 2007
    Location
    Reading
    Posts
    5,038
    Thank Post
    160
    Thanked 909 Times in 713 Posts
    Blog Entries
    3
    Rep Power
    270
    I know this can be done, i have seen it done by a friend i used to work with.

    He was abit of a Macro Genius, will give him a call see if i can get the info for you.

    Regards

    James.

  3. #3

    SYNACK's Avatar
    Join Date
    Oct 2007
    Posts
    11,060
    Thank Post
    853
    Thanked 2,675 Times in 2,269 Posts
    Blog Entries
    9
    Rep Power
    768
    This does it for a single instance, just need some loop code to get it to do the whole lot at once:

    Code:
        Selection.Find.ClearFormatting
        With Selection.Find
            .Text = "@"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute
        Selection.SelectCell
        With Selection.Cells
            With .Shading
                .Texture = wdTextureNone
                .ForegroundPatternColor = wdColorAutomatic
                .BackgroundPatternColor = wdColorYellow
            End With
        End With
    It will search the whole document though, not just cells.

    This may help to, looping code for find:
    http://visualbasic.about.com/library...ecvbai0303.htm
    Last edited by SYNACK; 26th June 2008 at 05:07 PM.

  4. #4

    Join Date
    Aug 2005
    Location
    London
    Posts
    3,154
    Thank Post
    114
    Thanked 527 Times in 450 Posts
    Blog Entries
    2
    Rep Power
    123
    You can loop through the collection of tables and then do the search/replace on the contents of each table - I think that would work OK.

    Is this just a one-off? If so, you can just use ordinary find/replace to specify a colour for the replacement text - this kind of thing is also handy when you want to change (say) 10 point Arial to 12 point Times Bold and you were evil enough to not use styles :-)

  5. #5

    Join Date
    Nov 2006
    Location
    Lancashire
    Posts
    95
    Thank Post
    34
    Thanked 1 Time in 1 Post
    Rep Power
    0
    Quote Originally Posted by SYNACK View Post
    This does it for a single instance, just need some loop code to get it to do the whole lot at once:
    Thank you for that - you are right, that works perfectly for a single instance and every time you run it, it finds another cell to colour (until all the symbols have been coloured.

    I need this to loop now until it has coloured all the cells...

    as you will have guessed, I am a complete novice at VB and trying to learn it on the fly. I think I need a 'Do While' or 'Do Until' expression, but am unsure of what statement I need to stop it looping.

    I need something like - Do Until {can't find '@'}

    Can you suggest how I might do this please?

    Thanks again.
    Last edited by park_bench; 30th June 2008 at 02:36 PM.

SHARE:
+ Post New Thread

Similar Threads

  1. Add dummy table to censornet
    By gh256 in forum How do you do....it?
    Replies: 0
    Last Post: 10th December 2007, 02:24 PM
  2. Time table
    By thegrassisgreener in forum General Chat
    Replies: 35
    Last Post: 27th November 2007, 04:39 PM
  3. PHP Loop Table
    By Jackd in forum Web Development
    Replies: 2
    Last Post: 25th November 2007, 12:28 AM
  4. Excel Not Recognizing Cells of Equal Value
    By G_Money in forum Windows
    Replies: 2
    Last Post: 21st June 2007, 08:33 AM
  5. SQL Table Location G&T
    By mark80 in forum MIS Systems
    Replies: 2
    Last Post: 16th May 2007, 12:46 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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