FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Search and coloring of a phrase
Posts: 1392
Joined: Mon May 14, 2007 09:49 AM
Search and coloring of a phrase
Posted: Sun Aug 13, 2023 05:44 AM
Hi,

To find and color a phrase in the text of a Word table cell, I do this:
Code (fw): Select all Collapse
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 ?
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: Search and coloring of a phrase
Posted: Sun Aug 13, 2023 08:01 AM
Dear Yuri,
Code (fw): Select all Collapse
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
Posts: 1392
Joined: Mon May 14, 2007 09:49 AM
Re: Search and coloring of a phrase
Posted: Sun Aug 13, 2023 09:09 AM

Thank you, Antonio! Great

Continue the discussion