Search and coloring of a phrase

Post Reply
Natter
Posts: 1226
Joined: Mon May 14, 2007 9:49 am

Search and coloring of a phrase

Post by Natter »

Hi,

To find and color a phrase in the text of a Word table cell, I do this:

Code: Select all | Expand

oTb:Item(n).Cell(x,y):Select()
oWrd:Selection.Find:Execute("MyText", .F., .T.)
oWrd:Selection.Range:HighLightColorIndex=CLR_RED
How can this be done if the text contains several identical phrases ?
User avatar
Antonio Linares
Site Admin
Posts: 42268
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: Search and coloring of a phrase

Post by Antonio Linares »

Dear Yuri,

Code: Select all | Expand

PROCEDURE ColorOccurrencesInTableCells()
    LOCAL oWord, oTable, oCell, oRange
    LOCAL n, x
    
    oWord := CreateObject("Word.Application")
    oWord:Visible := .T.  // Optional: Make Word application visible
    
    oTable := oWord:ActiveDocument:Tables(1)  // Modify as needed
    
    FOR n := 1 TO oTable:Rows:Count
        FOR x := 1 TO oTable:Columns:Count
            oCell := oTable:Cell(n, x)
            oRange := oCell:Range
            oRange:Collapse(0)  // Start from the beginning of the cell
            
            DO WHILE oRange:Find:Execute("MyText")
                oRange:HighlightColorIndex := 6  // Red color index
                oRange:Collapse(0)  // Move to the end of found text
            ENDDO
        NEXT
    NEXT
    
    oRange := NIL
    oWord:Visible := .F.  // Hide Word application
    oWord := NIL
RETURN
regards, saludos

Antonio Linares
www.fivetechsoft.com
Natter
Posts: 1226
Joined: Mon May 14, 2007 9:49 am

Re: Search and coloring of a phrase

Post by Natter »

Thank you, Antonio! Great
Post Reply