Search multiples words & highlight in openoffice writer doc

Creating a macro - Writing a Script - Using the API

Search multiples words & highlight in openoffice writer doc

Postby daki » Fri Apr 26, 2019 9:22 pm

1) F&R option not required - Does not fit my requirment to search from a list of 1500 words
2)Blacklist option not required - Does not fit my requirment to search from a list of 1500 words

In need of macro for open office writer as simlar to below macro for microsoft word,

This macro for word document searches all the words that will be listed in another document.

Steps
1.put all words you want to search in one document, phrase should occupy one paragraph
2.Now there is a box. Enter the path and name of the list document you just saved. Then click “OK”
3.The next thing you see is all found words are in highlighting.

Macros for Microsoft word as beleow, Need code of such for openoffice writer.

Code: Select all   Expand viewCollapse view
Sub FindMultiItemsInDoc()
  Dim objListDoc As Document
  Dim objTargetDoc As Document
  Dim objParaRange As Range, objFoundRange As Range
  Dim objParagraph As Paragraph
  Dim strFileName As String

  strFileName = InputBox("Enter the full name of the list document here:")

  Set objTargetDoc = ActiveDocument
  Set objListDoc = Documents.Open(strFileName)
  objTargetDoc.Activate

  For Each objParagraph In objListDoc.Paragraphs
    Set objParaRange = objParagraph.Range
    objParaRange.End = objParaRange.End - 1

      With Selection
        .HomeKey Unit:=wdStory

        '  Find target items.
        With Selection.Find
          .ClearFormatting
          .Text = objParaRange
          .MatchWholeWord = True
          .MatchCase = False
          .Execute
        End With

        '  Highlight the found items.
        Do While .Find.Found
          Set objFoundRange = Selection.Range
          objFoundRange.HighlightColorIndex = wdBrightGreen
          .Collapse wdCollapseEnd
          .Find.Execute
        Loop
      End With
  Next objParagraph
End Sub


Pl help share such macro code for open office writer, Thank you.
Last edited by robleyd on Sat Apr 27, 2019 2:25 am, edited 1 time in total.
Reason: Add CODE tags
openoffice 4.1.6
daki
 
Posts: 1
Joined: Fri Apr 26, 2019 9:09 pm

Re: Search multiples words & highlight in openoffice writer

Postby FJCC » Sat Apr 27, 2019 6:11 pm

Very lightly tested. I stored the code in My Macros and used Tools -> Macros -> Run Macro to run it.
Code: Select all   Expand viewCollapse view
Sub WordSearch
oDoc = ThisComponent
PATH = InputBox("Enter full path of word bank.", "File Path", "/home/fjcc/Desktop/WordBank.odt")
oBank = StarDesktop.loadComponentFromURL(convertToURL(PATH), 0, "_blank", Array())
oTextBank = oBank.Text

oDesc = oDoc.createSearchDescriptor()
oDesc.searchWords = TRUE

oParaEnum = oTextBank.createEnumeration()
While oParaEnum.hasMoreElements()
   oPara = oParaEnum.nextElement()
   oDesc.SearchString = oPara.String
   oFound = oDoc.findAll(oDesc)
   If oFound.Count > 0 Then
      For i = 0 to oFound.Count -1
         oInstance = oFound.getByIndex(i)
         oInstance.CharColor = RGB(255,0,0) `Make text red
      next i
   End If
WEnd
oBank.close(True)
   
End Sub
Windows 10 and Linux Mint, since 2017
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
 
Posts: 7261
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Search multiples words & highlight in openoffice writer

Postby JeJe » Sat Apr 27, 2019 7:28 pm

For the nearest OO has to highlight color use oInstance.charbackcolor in FJCC's code instead of oInstance.CharColor.

oInstance.charbackcolor=qbcolor(10) will give a light green background
Openoffice 4.1.2
Windows 8
JeJe
Volunteer
 
Posts: 575
Joined: Wed Mar 09, 2016 2:40 pm


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 6 guests