X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
   Reply to this topicStart new topic
> Highlight Words In A Table Cell, Office 2010    
post May 8 2018, 01:54 PM

Posts: 343
Joined: 14-October 10
From: Southeastern Pennsylvania

I have a Word document containing a table. There is a cell in the table containing several sentences. Using VBA, I need to find all occurrences of the word "bird" (in this cell) and highlight each of them.

Let's say it's table 2, row 5, cell 3.

Sample VBA code would be appreciated.

Thanks in advance.
Go to the top of the page
post May 8 2018, 03:56 PM

Posts: 18,324
Joined: 29-March 05
From: Wisconsin


At first I thought you wanted to just highlight the whole cell if the word "bird" appeared in the cell, but after re-reading your post, it looks much more complicated. But not bad. Give this a try:

Public Sub HighlightWordsInSentenceInTable()

    Dim tbl As Table, intWordLocation As Integer, strWord As String
    Dim intRow As Integer, intCol As Integer
'   Customize these bits as needed
    Set tbl = ActiveDocument.Tables(1)
    strWord = "bird"
    intRow = 5
    intCol = 3
'   Select the cell's text
    tbl.Cell(intRow, intCol).Range.Select
'   Move cursor to the start of the cell's text
    Selection.MoveLeft unit:=wdCharacter, Count:=1
'   Find the first occurrence of the word (any case; bird, Bird, BIRD, or even bIrD)
    intWordLocation = InStr(1, UCase(tbl.Cell(intRow, intCol).Range.Text), UCase(strWord))
'   As long as we keep finding occurrences of the word, keep looping...
    While intWordLocation > 0
'       Move the cursor to the start of the word
        Selection.MoveRight unit:=wdCharacter, Count:=intWordLocation - 1

'       Select the word
        Selection.MoveRight unit:=wdCharacter, Count:=Len(strWord), Extend:=wdExtend

'       Highlight the word in Yellow
        Selection.Shading.BackgroundPatternColor = wdColorYellow

'       Reset the selection to the start of the cell
        tbl.Cell(intRow, intCol).Range.Select
        Selection.MoveLeft unit:=wdCharacter, Count:=1

'       Find the next occurrence of the word
        intWordLocation = intWordLocation + Len(strWord)
        intWordLocation = InStr(intWordLocation, UCase(tbl.Cell(intRow, intCol).Range.Text), UCase(strWord))

    Set tbl = Nothing

End Sub

The basic concept behind this is that you can use the InStr() function to find each occurrence of a string. If you find the string once, you can use that position to move your starting point up, so the next time it skips over the first one, and so on and so on. After that, it's just a matter of figuring out the syntax for referring to a table/cell/the contents of that cell.

Hope this helps,

Go to the top of the page
post May 8 2018, 08:06 PM

Posts: 343
Joined: 14-October 10
From: Southeastern Pennsylvania


It works beautifully.

Thanks very much.
Go to the top of the page

Custom Search
RSSSearch   Top   Lo-Fi    26th September 2018 - 02:17 AM