Accessing Built in Word Count

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
UbuntuUser
Posts: 17
Joined: Tue Mar 22, 2016 1:25 pm

Accessing Built in Word Count

Post by UbuntuUser »

Dear ye learned in OO ,

I had being enjoying the use of a seemingly fantastic BASIC function by Andrew Brown. I am using it to count the words in each page. Unfortunate it is not accurate or consistent enough between pages for my needs. I tested OO's built in word count and was delighted to see that the selection count was very consistent. I basically want to tap into it and give it either a range or a string and have it return the word count as an integer. is this possible?

Bellow is the function by Andrew Brown. it does work, but just not for my needs.

Code: Select all

Function hotcount(aString)
'*******************************************
	'Function: Count Words in provided string
	'Author: Andrew Brown

	'Last updated 18 March 2016
	'Last updated by:Daniel Wilson
'*******************************************
	' the ultimate, using the same breakiterator as the program
	'
	Dim mystartpos As Long
	Dim numwords,nw
	Dim nextwd As New com.sun.star.i18n.Boundary
	Dim aLocale As New com.sun.star.lang.Locale
	Dim brk
	aLocale.Language="en"
	' Urska, you may need to change the line above.
	numwords=1 ' don't ask me why we need this
	mystartpos=0
	brk=CreateUNOService("com.sun.star.i18n.BreakIterator")
	nextwd=brk.nextWord(aString,startpos,aLocale,com.sun.star.i18n.WordType.WORD_COUNT)
	Do While nextwd.startPos<> nextwd.endPos
		numwords=numwords+1
		nw=nextwd.startpos
		nextwd=brk.nextWord(aString,nw,aLocale,com.sun.star.i18n.WordType.WORD_COUNT)
	Loop
	hotcount=numwords
End Function
With thanks,
Last edited by UbuntuUser on Mon Apr 04, 2016 4:45 am, edited 2 times in total.
OpenOffice 4, Ubuntu 14.04
User avatar
RoryOF
Moderator
Posts: 35064
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Accessing Built in Word Count

Post by RoryOF »

There is an updated macro for Word Count at
http://extensions.openoffice.org/en/pro ... controller
This appears on the Status Bar; I think it gave count for the current selection but I don't have it installed as I don't need a running word count, so cannot check.
 Edit: There are existing macros to return selected File Properties for the file in use. A quick Google should find these. Look, for example, at /Tools /Macros /Organise Macros /OpenOffice Basic; in Library Tools : Debug : ShowPropertyValues 
Apache OpenOffice 4.1.15 on Xubuntu 22.04.5 LTS
UbuntuUser
Posts: 17
Joined: Tue Mar 22, 2016 1:25 pm

Re: Accessing Built in Word Count

Post by UbuntuUser »

Thank you for your response! I had a good look through all the OO macros and did some searching on Google before posted here. I Wasn't able to find much. All the topics I could find where referring to simply displaying the word count automatically. I installed the Extension you suggested but I couldn't find a way to use it to do a word count on a range. I also got the function going. It does show many Interesting properties, but none of them are a word count. any ideas?
OpenOffice 4, Ubuntu 14.04
User avatar
RoryOF
Moderator
Posts: 35064
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Accessing Built in Word Count

Post by RoryOF »

The code for this macro can be accessed under /Tools /Macros /Organise Macros /Beanshell. When this extension is installed, if one selects text in Writer the Status Bar display shows the word count for selection and the total count for document (selection/total). It should be relatively simple to amend the existing macro code and divert these values to your own customised routine.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.5 LTS
UbuntuUser
Posts: 17
Joined: Tue Mar 22, 2016 1:25 pm

Re: Accessing Built in Word Count

Post by UbuntuUser »

Thank you! I'll see if i can adapt the code for my own needs!

For the benefit of others who may read this, I was not able to open the java editor on Ubuntu 14.04. i found the text file in /opt/openoffice4/share/Scripts/beanshell/WordCount. You may use a text editor access the script. Save the text file to .openoffice/4/user/Scripts/beanshell
OpenOffice 4, Ubuntu 14.04
Post Reply