+ Reply to Thread
Results 1 to 3 of 3

Conditional Formatting in ms Word for specific cells in ms Word table using VBA code??

  1. #1
    Registered User
    Join Date
    10-29-2014
    Location
    Cardiff
    MS-Off Ver
    2010
    Posts
    2

    Conditional Formatting in ms Word for specific cells in ms Word table using VBA code??

    Hi,

    I am new to VBA programming and I'm sure that this is where the solution lies to my current problem...

    I am using the "Mail Merge" option in ms Word to bring in information from a table created in Excel. Information such as Name, Surname etc. but other information is also required.

    The imported information must be presented in the form of a table in Word (but cannot be an Excel table). I am looking for a VBA code/answer to this.

    The issue lies in the fact that the letter "Y" denotes to a particular colour in a particular cell. Conditional formatting isn't available in ms Word.

    The following photos hopefully explain my problem better. I need the word document to resemble the colour as well as the letter.

    Many thanks!

    Excel Table:
    Excel_photo.png

    Word Table:
    Word_photo.png

  2. #2
    Registered User
    Join Date
    10-29-2014
    Location
    Cardiff
    MS-Off Ver
    2010
    Posts
    2

    Re: Conditional Formatting in ms Word for specific cells in ms Word table using VBA code??

    I think I'm getting there - but what's wrong with the following?

    Public Sub Coch()

    Dim tbl As Table

    If ActiveDocument.Tables(1).Cell(4, 3).ID = y Then
    ActiveDocument.Tables(1).Cell(4, 3).Shading.BackgroundPatternColor = wdColorRed

    Else
    If Not ActiveDocument.Tables(1).Cell(4, 3).ID = y Then
    ActiveDocument.Tables(1).Cell(4, 3).Shading.BackgroundPatternColor = wdWhite

    End If
    End If


    End Sub


    If any other letter is in Cell (4,3) the cell is still highlighted red. I want the cell coloured white if empty.

    Thanks.

  3. #3
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Conditional Formatting in ms Word for specific cells in ms Word table using VBA code??

    You can do this as part of the mailmerge itself, with no need for post-merge VBA processing.

    Although Word doesn’t have tools for the kind of conditional cell formatting one finds in Excel, the effect can be simulated via field coding that combines tab-stops and manual line breaks inside an IF test that conditionally outputs the various strings. Such a field might be coded like:
    {IF«myData»= "Y" {QUOTE "^t^t^l
    ^t«myData»^t^l
    ^t^t" \* Charformat}}
    where ^t represents a TAB, ^l represents a manual line break and {Q is the character you format with the required background shading (and any other desired font attributes).

    The foregoing construction could be used to output the 'Y', horizontally and vertically centred in the cell. The centring is achieved by using centred tab-stops, plus right-aligned tab-stops to extend each line to the cell border. An empty line above and below the output string is also inserted. Obviously, the cell would need a fixed width and you would also need to specify a 0-width internal border. Variations on the theme could be used to accommodate other data and cell formats. Via multiple IF tests, you could have as many as 16 conditional formats - one for each highlight option - plus however many different font attributes you might desire.

    How's that for flexibility - and not a line of VBA code in sight!

    Note: The field brace pairs (i.e. '{ }') for the above example are created in the document itself, via Ctrl-F9 (Cmd-F9 on a Mac); you can't simply type them or copy & paste them from this message. Nor is it practicable to add them via any of the standard Word dialogues. Likewise, you can't type or copy & paste the chevrons (i.e. '« »') - they're part of the actual mergefields, which you can insert from the mailmerge toolbar. The spaces represented in the field construction are all required.
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 4
    Last Post: 02-23-2014, 08:18 AM
  2. Formatting Word table cells
    By tania19b in forum Word Formatting & General
    Replies: 1
    Last Post: 01-13-2013, 07:16 PM
  3. Replies: 1
    Last Post: 08-17-2012, 08:16 PM
  4. VBA Trying to pull specific Word table cells into specific Excel cells
    By ez08mbba in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 03-16-2012, 01:11 PM
  5. [SOLVED] search for a specific word and copy the word and the preceeding words until a comma
    By DHANANJAY in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 12-31-2005, 09:10 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1