Search and coloring of a phrase

Search and coloring of a phrase

Postby Natter » Sun Aug 13, 2023 5:44 am

Hi,

To find and color a phrase in the text of a Word table cell, I do this:
Code: Select all  Expand view
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 ?
Natter
 
Posts: 1120
Joined: Mon May 14, 2007 9:49 am

Re: Search and coloring of a phrase

Postby Antonio Linares » Sun Aug 13, 2023 8:01 am

Dear Yuri,

Code: Select all  Expand view
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
User avatar
Antonio Linares
Site Admin
 
Posts: 41366
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Search and coloring of a phrase

Postby Natter » Sun Aug 13, 2023 9:09 am

Thank you, Antonio! Great
Natter
 
Posts: 1120
Joined: Mon May 14, 2007 9:49 am


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 54 guests