Linux: must-have macro

I got sick and tired of not having the ability to do a word count on a selected portion of text, and I knew there were various macros out there for OpenOffice, so I finally got off my rear and experimented with a few. The problem, as usual, is that the technoids who graciously donate their time and effort tend not to realize that most of us need things explained from the ground up. And that’s ground as in actual ground, terra firma, not somewhere on the third floor.

Anyhow, if you select Tools / Macro -> Macro from the pull-downs, a confusing window will pop up. On the left side, click once on Standard so that the macro will show up in all your text documents, then click on the New button on the right side of the window. A new window will pop up with some Basic in it, just clear it all out and paste in the italicized Basic code below, then save it by selecting File / Save. To run it, select a piece of text, select Tools / Macro -> Macro, highlight acbwc by clicking once on it, then click Run. If you want to get fancy, of course, you can just add that macro to a button or whatever. It’s fast and it works well, and hopefully will be built into the next version of OpenOffice. So, a big thanks to the two Andrews, Brown and Pitonyak.

Once you’ve added the acbwc Word Count macro, here’s how to add a button to run the macro to the toobar:

1. Select Tools/Configure

2. Select Function Bar, then click on the Customize button

3. Click on the + next to BASIC Macros

4. Click on the + next to Standard, then again for Module 1

5. Select acbwc, then click on the Add–> button.

6. Select acbwc on the right window, then click the Icons button to give it an icon. Make sure that the checkbox is checked, or it won’t show up on the toolbar.

Sub acbwc

‘ v2.0.1

‘ 5 sept 2003

‘ does footnotes and selections of all sizes

‘ still slow with large selections, but I blame Hamburg 🙂

‘ v 2.0.1 slightly faster with improved cursorcount routine

‘ not unendurably slow with lots of footnotes, using cursors.

‘ acb, June 2003

‘ rewritten version of the

‘ dvwc macro by me and Daniel Vogelheim

‘ september 2003 changed the selection count to use a word cursor for large selections

‘ following hints from Andrew Pitonyak.

‘ this is not perfect, either, largely because move-by-word is erratic.

‘ it will slightly exaggerate the number of words in a selection, counting extra

‘ for paragraph ends and some punctuation.

‘ but it is still much quicker than the old method.

Dim xDoc, xSel, nSelcount

Dim nAllChars

Dim nAllWords

Dim nAllPars

Dim thisrange, sRes

Dim nSelChars, nSelwords, nSel

Dim atext, bigtext

Dim fnotes, thisnote, nfnotes, fnotecount

Dim oddthing,startcursor,stopcursor

xDoc = thiscomponent

xSel = xDoc.getCurrentSelection()

nSelCount = xSel.getCount()


‘ by popular demand …


If fnotes.hasElements() Then


For nfnotes=0 To fnotes.getCount()-1




Do While thisnote.getText().compareRegionStarts(startcursor,stopcursor) AND _




‘ msgbox(startcursor.getString())

‘ fnotecount=fnotecount+stringcount(thisnote.getstring())

‘ fnotecount=fnotecount+CursorCount(thisnote,bigtext)

Next nfnotes

End If

‘ this next “If” works around the problem that If you have made a selection, then

‘ collapse it, and count again, the empty selection is still counted,

‘ which was confusing and ugly

If nSelCount=1 and xSel.getByIndex(0).getString()=”” Then


End If

‘ access document statistics

nAllChars = xDoc.CharacterCount

nAllWords = xDoc.WordCount

nAllPars = xDoc.ParagraphCount

‘ initialize counts

nSelChars = 0

nSelWords = 0

‘ the fancy bit starts here

‘ iterate over multiple selection

For nSel = 0 To nSelCount – 1



If len(atext)< 220 Then



End If


Next nSel

‘ dialog code rewritten for legibility

If fnotes.hasElements() Then

sRes=”Document count (with footnotes): ” + nAllWords + ” words. ” + chr(13)

sRes= sRes + “Word count without footnotes: ” + str(nAllWords-fnotecount) +_

” words. ” + chr(13)+”(Total: ” +nAllChars +” Chars in ”


sRes= “Document count: ” + nAllWords +” words. ” + chr(13)+”(” + _

nAllChars +” Chars in ”

End If

sRes=sRes + nAllPars +” Paragraphs.)”+ chr(13)+ chr(13)

If nselCount>0 Then

sRes=sRes + “Selected text count: ” + nSelWords + ” words” + chr(13) +_

“(” + nSelChars + ” chars”

If nSelcount=1 Then

sRes=sRes + ” In ” + str(nselCount) + ” selection.)”


REM I don’t know why, but need this adjustment

sRes=sRes + ” In ” + str(nselCount-1) +” selections.)”

End If

sRes=sRes+chr(13)+chr(13)+”Document minus selected:” + chr(13)+_

str(nAllWords-nSelWords) + ” words.” +chr(13) +chr(13)

End If

If fnotes.hasElements() Then

sRes=sRes+”There are “+ str(fnotecount) + ” words in “+ fnotes.getCount() +_

” footnotes.” +chr(13) +chr(13)

End If

msgbox(sRes,64,”acb Word Count”)

End Sub

function Cursorcount(aRange)

‘ acb September 2003

‘ quick count for use with large selections

‘ based on Andrew Pitonyak’s WordCountWordCursor() function

‘ but made cruder, in line with my general tendency.

Dim lnumwords as long

Dim atext

Dim startcursor, stopcursor as object



If not atext.compareRegionStarts(aRange.getStart(),aRange.getEnd()) Then






End If

Do while aText.compareRegionEnds(startCursor, stopcursor) >= 0 and _





end function

Function stringcount(astring)

‘ acb June 2003

‘ slower, more accurate word count

‘ for use with smaller strings

‘ sharpened up by David Hammerton ( in September 2003

‘ to allow those who put two spaces after punctuation to escape their just deserts

Dim nspaces,i,testchar,nextchar


For i= 1 To len(astring)-1


select Case testchar

Case ” “,chr(9),chr(13)

nextchar = mid(astring,i+1,1)

select Case nextchar

Case ” “,chr(9),chr(13),chr(10)


Case Else


end select

end select

Next i


end function