Sub dvwc
' Daniel Vogelheim wrote this
' word count macro, adopted from http://www.darwinwars.com/lunatic/bugs/oo_macros.html#swc
' displays a message box with number of characters and words in the current selection, and in the document
'
' I added that
' because I don't care about character counts, I have stripped them out:
' if you want them, replace the commented-out line at the bottom
' acb 12/6/2002
' original header:
' horrid word count macro by Andrew Brown
' adapted from Werner Roth
' who wrote a macro in the old starbasic to do this
' all I did was to wrap his logic in the new API
' this counts the words in a selected patch of text
'
' Released by www.ooextras.org under the LGPL license.
' See http://www.ooextras.org/license.txt for terms of license
' Original homepage: http://www.darwinwars.com/lunatic/bugs/oo_macros.html
' get the document, and the currently selected text
xDocument = thiscomponent
xSelection = xDocument.getCurrentSelection()
nSelCount = xSelection.getCount()
' access document statistics
nAllChars = xDocument.CharacterCount
nAllWords = xDocument.WordCount
' initialize counts
nSelChars = 0
nSelWords = 0
' iterate over multiple selection
for nSel = 0 to nSelCount - 1
sText = xSelection.getByIndex(nSel).getString()
' count word in sText by scanning the selected text character for character
nCount = Len(sText)
bLastWasseparator = true 'first letter starts a word
for i=1 to nCount
Select Case Mid(sText,i,1)
'Add your own separators here
'chr(9) is a tab
Case " ", "(", ")", chr(9)
bLastWasseparator = true
nSelChars = nSelChars + 1
'chr(10) and chr(13) are for Line- and Paragraph-ends
Case chr(10), chr(13)
bLastWasseparator = true
'Character found, so this is no separator
Case Else
'Increase the number of words only if the last character was a separator
'(i.e., if we're at the start of a word)
if bLastWasseparator then
nSelWords = nSelWords + 1
Endif
nSelChars = nSelChars + 1
bLastWasseparator = false
End Select
next i
next nSel
' Daniel Vogelheim's messagebox is the line commented out here. Mine is the uncommented one below it
' msgbox "Document" + chr(13) + " chars: " + nAllChars + chr(13) + " words: " + nAllWords + chr(13) + chr(13) + "Selection" + chr(13) + " chars: " + nSelChars + chr(13) + " words: " + nSelWords, 64, "Word counts in Document"
msgbox "Document count:" + chr(13) + nAllWords +" words. " + chr(13) + chr(13) + "Selected text count:" + chr(13) + nSelWords + " words" + chr(13) + "(character count: " + nSelChars + ")", 64, "Words in Document"
End Sub