Skip to main content.

Fanatic Attack is about entrancement, entertainment, and an enhancement of curiosity.

 

Adding Visual Word Count to OpenOffice.org

category: Open Source

Although OpenOffice.org has a competent word count feature, it lacks one nifty trick: it can show you how many words and characters you’ve already written, but it won’t tell you how close you are to the target. Fortunately, you can write a simple OpenOffice.org macro that not only shows you how many percent of the work are already completed but also presents this data as a nice progress bar. When you run the macro it prompts you to enter the target word count. It then calculates the percentage based on the current word count and displays this data as a progress bar.

by Dmitri Popov


Before you get your hands dirty with coding, you have to create a dialog box containing two numeric fields — NumericField1 and NumericField2 — and the ProgressBar1 progress bar. Give the created dialog box the WordCountDialog name.

Visual Word Count Dialogue

Caption: Creating a dialogue for the Visual Word Count macro

Once you’re done with the dialog box, you can start working on the code. The macro starts with obtaining the word count of the currently opened document using the WordCount property:

ThisDoc=ThisComponent

CurrentCount=ThisDoc.WordCount

It then displays an input box, prompting the user to enter the target word count:

TargetCount=InputBox(“Enter target word count:”, “Input Box”)

If the user doesn’t enter anything in the input box, the macro quits:

If TargetCount=”" Then End

This provides an easy way out of the macro in case the user has changed her mind. Next, the macro opens the created dialog box:

OpenDialog(”WordCountDialog”)

Dialog=CreateUnoDialog(TheDialog)

It then sets the NumericField1 to the current word count and calculates the percentage of the actual word count in relation to the specified target word count:

NumericField1=Dialog.GetControl(”NumericField1″).setValue(CurrentCount)

Value=Int((CurrentCount/TargetCount)*100)

The Int routine rounds the calculated percentage, which makes it possible to avoid values like 72.6. Finally, the macro generates a progress bar based on the calculated percentage value:

Bar=Dialog.GetControl(”ProgressBar1″)

Bar.Value=Value

That’s all there is to it. Here is the macro in all its beauty for your perusal:

Sub VisualWordCount()

ThisDoc=ThisComponent

CurrentCount=ThisDoc.WordCount

TargetCount=InputBox(InputTargetWordCount, InputWordCount)

If TargetCount=”" Then End

OpenDialog(”WordCountDialog”)

Dialog=CreateUnoDialog(TheDialog)

NumericField1=Dialog.GetControl(”NumericField1″).setValue(CurrentCount)

Value=Int((CurrentCount/TargetCount)*100)

Bar=Dialog.GetControl(”ProgressBar1″)

Bar.Value=Value

NumericField2=Dialog.getControl(”NumericField2″).setValue(Value)

Dialog.Execute

End Sub

And here is the macro in action:

Visual Word Count

Caption: The Visual Word Count macro in action

If you don’t feel like writing the described solution from scratch, you might want to download the Writer’s Tools extension (http://code.google.com/p/writertools) which contains the Visual Word Count feature. Feel free to dissect and study it.

by Dmitri Popov of Nothickmanuals.info

Posted by FA Editors at 9:29 AM PDT

comments

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>