InDesign 2.0: Word Count using Visual Basic


Adding a Word Count to InDesign 2.0

TextCounter (Win) on Adobe Studio Exchange

One of the features missing from InDesign since its first version is a word count feature. In a fit of quiet rage and sheer stupidity, I wrote this simple Visual Basic 6.0 application that pops up a small window displaying the word count of the current selected story.

There is a Timer named tLexLuthor that waits around for a period of time

[1008] 01 lois lane timer

Just in case the user doesn’t want this turned on, there is a status button called bStart that toggles the timer tLexLuthor on and off.

[1009] 02 lois lane button

And finally there is a status field (read only) called lStatus. This is where the current word count is stored.

[1010] 03 lois lane label status

… And when running, this is how it looks.

[1011] 04 lois lane in action

The Visual Basic source that sits behind this small application is as follows:


Dim myInDesign As InDesign.Application

Dim myPub As InDesign.Document

'button named bStart (toggles on/off of Timer)

'lStatus as a label that contains the word count text

'tLexLuthor as a Timer object

Private Sub bStart_Click()
If (bStart.Caption = "Start") Then
bStart.Caption = "Stop"
tLexLuthor.Enabled = True
lStatus.Caption = ""
Else
bStart.Caption = "Start"
tLexLuthor.Enabled = False
lStatus.Caption = "waiting"
End If

End Sub

Private Sub tLexLuthor_Timer()
processEvent

End Sub

Private Sub processEvent()
Dim mySel As Object 'untyped object because we don't know what the selection is
On Error GoTo fail 'if anything goes wrong, don't call me
Set myInDesign = CreateObject("InDesign.Application")
Set myPub = myInDesign.ActiveDocument
Set mySel = myPub.Selection(1) 'a selection can be multiple objects, this time just get the first (and potentially only)
If (TypeOf mySel(1) Is InDesign.TextFrame) Then 'if it is a text frame, then
myCount = mySel(1).ParentStory.TextWords.Count 'get the word count from the text frame
ElseIf (TypeOf mySel(1) Is InDesign.InsertionPoint) Then 'or if its a insertion point, that is the user is typing
myCount = mySel(1).ParentTextFrame.ParentStory.TextWords.Count 'get the word count through the frame's text object
End If
lStatus.Caption = "word count: " & CStr(myCount) 'set the status
Exit Sub

fail:
lStatus.Caption = ""

End Sub

The project and executable is here:Lois Lane example downloadable (3K)

2 thoughts on “InDesign 2.0: Word Count using Visual Basic”

Comments are closed.