UtterAccess.com
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    
 
   
LonesomeDove
post May 8 2018, 01:54 PM
Post#1



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.

--------------------
LonesomeDove
"There is great satisfaction in building good tools for other people to use." - Freeman Dyson
Go to the top of the page
 
doctor9
post May 8 2018, 03:56 PM
Post#2


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


LonesomeDove,

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:

CODE
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))
    Wend

    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,

Dennis

--------------------
(;,;) Li'l Cthulu says: Please talk about what you're trying to do, as well as how you're doing it.
Changing your real table name to "Table1" and your real form name to "Form1" in your posts makes it more difficult to understand what's going on, not easier.
Guidelines for Posting Questions
Go to the top of the page
 
LonesomeDove
post May 8 2018, 08:06 PM
Post#3



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


Dennis,

It works beautifully.

Thanks very much.

--------------------
LonesomeDove
"There is great satisfaction in building good tools for other people to use." - Freeman Dyson
Go to the top of the page
 


Custom Search
RSSSearch   Top   Lo-Fi    27th May 2018 - 12:29 PM