About Me

microformats hcard approaching

is a Professional Geek for Microsoft Australia. More info lives underneath the About Box...

-33.831416, 151.222526
MrCell+61.417.212181
Work:
1 Epping Road
North Ryde, NSW 2113
Australia
photo of nick hodge

Stuff

View Nick Hodge's profile on LinkedIn

msdn channel 9

InDesign 2.0: Word Count using Visual Basic

By Nick Hodge | January 7, 2002

Adding a Word Count to InDes­ign 2.0

TextCounter (Win) on Adobe Stu­dio Exchange

One of the fea­tures miss­ing from InDes­ign since its first ver­sion is a word count fea­ture. In a fit of quiet rage and sheer stu­pid­ity, I wrote this simple Visual Basic 6.0 applic­a­tion that pops up a small win­dow dis­play­ing the word count of the cur­rent selec­ted 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 but­ton 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 cur­rent word count is stored.

[1010] 03 lois lane label status

… And when run­ning, this is how it looks.

[1011] 04 lois lane in action

The Visual Basic source that sits behind this small applic­a­tion 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 pro­ject and execut­able is here:Lois Lane example down­load­able (3K)

Topics: mungenet | 2 Comments »

2 Responses to “InDesign 2.0: Word Count using Visual Basic”

  1. InDesign 2.0 Scripts | nickhodge.com Says:
    September 5th, 2007 at 6:39 pm

    […] InDes­ign 2.0: Word Count using Visual Basic­Nick Hodge­Script­ing in InDes­ign 2.0 to add word count […]

  2. InDesign CS: Prepress Overview | nickhodge.com Says:
    September 6th, 2007 at 8:03 am

    […] only a word count: InDes­ign CS also counts sen­tences, lines and char­ac­ters. No more need for InDes­ign 2.0: Word Count using Visual Basic! The above image depicts a text frame that con­tains a cer­tain num­ber of characters/words etc, and […]

Comments