Page 1 of 1

Search multiples words & highlight in openoffice writer doc

Posted: Fri Apr 26, 2019 9:22 pm
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.

Re: Search multiples words & highlight in openoffice writer

Posted: Sat Apr 27, 2019 6:11 pm
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

Re: Search multiples words & highlight in openoffice writer

Posted: Sat Apr 27, 2019 7:28 pm
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

Re: Search multiples words & highlight in openoffice writer

Posted: Sun Dec 29, 2019 4:04 pm
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

Re: Search multiples words & highlight in openoffice writer

Posted: Sun Dec 29, 2019 8:20 pm
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


Re: Search multiples words & highlight in openoffice writer

Posted: Fri Aug 28, 2020 5:48 pm
by M4x_
JeJe wrote:Try this:
I really cannot get this working.

Re: Search multiples words & highlight in openoffice writer

Posted: Fri Aug 28, 2020 6:17 pm
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


Re: Search multiples words & highlight in openoffice writer

Posted: Fri Aug 28, 2020 7:53 pm
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"

Re: Search multiples words & highlight in openoffice writer

Posted: Sat Aug 29, 2020 8:22 pm
by M4x_
Many thanks, I'll have a look at it.

Re: Search multiples words & highlight in openoffice writer

Posted: Mon Aug 31, 2020 2:25 am
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

Re: Search multiples words & highlight in openoffice writer

Posted: Mon Aug 31, 2020 2:38 am
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

Re: Search multiples words & highlight in openoffice writer

Posted: Mon Aug 31, 2020 2:44 am
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