Search multiples words & highlight in openoffice writer doc

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
daki
Posts: 1
Joined: Fri Apr 26, 2019 9:09 pm

Search multiples words & highlight in openoffice writer doc

Post by daki »

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

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
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Search multiples words & highlight in openoffice writer

Post by FJCC »

Very lightly tested. I stored the code in My Macros and used Tools -> Macros -> Run Macro to run it.

Code: Select all

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
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Search multiples words & highlight in openoffice writer

Post by JeJe »

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
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
carryn@peachsa.co.za
Posts: 1
Joined: Sun Dec 29, 2019 3:40 pm

Re: Search multiples words & highlight in openoffice writer

Post by carryn@peachsa.co.za »

FJCC wrote:Very lightly tested. I stored the code in My Macros and used Tools -> Macros -> Run Macro to run it.
I can’t seem to get this working. Can anybody help me?

Code: Select all

Sub NeedlessWords()

oDoc = ThisComponent
oBank = Array("then", "almost", "about", "begin", "start", "decided", "planned", "very", "sat", "truly", "rather", "fairly", "really", "somewhat", "up", "down", "over", "together", "behind", "out", "in order", "around", "only", "just", "even")


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)
      next i
   End If
WEnd
oBank.close(True)

End Sub
Apache OpenOffice 4.1.7 with MacOS 10.15.2
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Search multiples words & highlight in openoffice writer

Post by JeJe »

Try this:

Code: Select all


Sub NeedlessWords()

oDoc = ThisComponent
oBank = Array("then", "almost", "about", "begin", "start", "decided", "planned", "very", "sat", "truly", "rather", "fairly", "really", "somewhat", "up", "down", "over", "together", "behind", "out", "in order", "around", "only", "just", "even")


oDesc = oDoc.createSearchDescriptor()
oDesc.searchWords = TRUE
otextbank =odoc.text
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)
      next i
   End If
WEnd

End Sub

Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
M4x_
Posts: 18
Joined: Wed Nov 07, 2018 8:40 am

Re: Search multiples words & highlight in openoffice writer

Post by M4x_ »

JeJe wrote:Try this:
I really cannot get this working.
OpenOffice 4.1.7
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Search multiples words & highlight in openoffice writer

Post by JeJe »

Me neither... so long ago I can't possibly remember what I was thinking.

Regex method:

Code: Select all


sub NeedlessWords()

oDoc = ThisComponent
oBank = Array("then", "almost", "about", "begin", "start", "decided", "planned", "very", "sat", "truly", "rather", "fairly", "really", "somewhat", "up", "down", "over", "together", "behind", "out", "in order", "around", "only", "just", "even")

for i =0 to ubound(obank) 'prepare regex string
if i<>0 then st = st & "|"
st = st & "\b" & obank(i) & "\b"
next

oDesc = oDoc.createSearchDescriptor()
odesc.SearchRegularExpression = true
odesc.searchstring = st
   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)
      next i
   End If

End Sub

Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Search multiples words & highlight in openoffice writer

Post by JeJe »

A little simpler for the regex expression

Code: Select all

for i =0 to ubound(obank)
if i<>0 then st = st & "|"
st = st & obank(i) 
next
st = "\b(" & st & ")\b"  '"\b(then|almost|about|)\b"
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
M4x_
Posts: 18
Joined: Wed Nov 07, 2018 8:40 am

Re: Search multiples words & highlight in openoffice writer

Post by M4x_ »

Many thanks, I'll have a look at it.
OpenOffice 4.1.7
M4x_
Posts: 18
Joined: Wed Nov 07, 2018 8:40 am

Re: Search multiples words & highlight in openoffice writer

Post by M4x_ »

JeJe wrote:A little simpler for the regex expression
Thanks again, and feel free to ignore my message since I'm perfectly fine with your first solution.
My list is quite small, so I just edited code and inserted my words.

With that said I hate to make personal requests, I hope it's not a big deal ...
would you mind adding the code for having highlight in red + bold please ?

I think next would be fantastic for all other users:
I was wondering if it's possible to search for a someway-linked list and not to single words written in the code.

Finally - this regarding my comprehension -
can I ask you please what improvements are offered by the second solution
and how to use it ?

Regards
Max
OpenOffice 4.1.7
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Search multiples words & highlight in openoffice writer

Post by JeJe »

I was wondering if it's possible to search for a someway-linked list and not to single words written in the code.
I don't know what you mean by that?

The second variation just made the Regex string a little simpler.

Code: Select all



sub Test

MakeWordsRedandBold Array("then", "almost", "about", "begin", "start", "decided", "planned", "very", "sat", "truly", "rather", "fairly", "really", "somewhat", "up", "down", "over", "together", "behind", "out", "in order", "around", "only", "just", "even")


end sub


sub MakeWordsRedandBold(oBank)

oDoc = ThisComponent

for i =0 to ubound(obank)
if i<>0 then st = st & "|"
st = st & obank(i)
next
st = "\b(" & st & ")\b"  '"\b(then|almost|about|)\b"

oDesc = oDoc.createSearchDescriptor()
odesc.SearchRegularExpression = true
odesc.searchstring = st
   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)
         oInstance.CharWeight =com.sun.star.awt.FontWeight.BOLD
      next i
   End If

End Sub
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Search multiples words & highlight in openoffice writer

Post by JeJe »

If you mean instead of using an array, pass the words in a single string then it would be this,
separating the words with a pipe symbol |

Code: Select all


sub Test2

MakeWordsRedandBold2 "then|almost|about" 


end sub


sub MakeWordsRedandBold2(st)

oDoc = ThisComponent

st = "\b(" & st & ")\b"  

oDesc = oDoc.createSearchDescriptor()
odesc.SearchRegularExpression = true
odesc.searchstring = st
   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)
         oInstance.CharWeight =com.sun.star.awt.FontWeight.BOLD
      next i
   End If

End Sub
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply