Sub WordCount 'Werner Roth 'This macro counts the number words in highlighted text. 'It scans the selected text and counts the appearances of separators 'Text in text boxes has to be selected separately. Otherwise it will be ignored. const MsgBoxErrorTitle = "Error Werner's WordCount" dim NumFound as long dim SelectedText as string dim LastWasSeperator as boolean 'First of all we do some error-handling: 'WordCound is designed to work just in StarWriter if ActiveModule.Name <> "StarWriter" then MsgBox("Werner's WordCount can just be used in text documents.",_ 16,MsgBoxErrorTitle) EXIT SUB Endif If (ActiveWindow.Selection.ClassName <> "Text") and _ (ActiveWindow.Selection.ClassName <> "TextInTable") then MsgBox("At least text or a table must be selected.",_ 16,MsgBoxErrorTitle) EXIT SUB Endif 'Assign the selected text to a string variable SelectedText = ActiveWindow.ValueAndBreaks 'Strings are limited to 64.000 Characters if SelectedText = "" then MsgBox("Either nothing or more than 64,000 characters is highlighted.",_ 16, MsgBoxErrorTitle) EXIT SUB Endif Application.EnterWait() 'show Hourglass-Cursor 'Number of found words NumFound = 0 'Two or more sequenced separators will be counted as one LastWasSeperator = false 'Scan the selected text character for character for i=1 to len(SelectedText) Select Case Mid(SelectedText,i,1) 'Add your own seperators here 'chr(9) is a tab 'chr(10) and chr(13) are for Line- and Paragraph-ends Case " ", ",", ";", ".", "-", chr(9), chr(10), chr(13) 'Increase the number of words just if the last character was not a seperator if not LastWasSeperator then NumFound = NumFound + 1 LastWasSeperator = true Endif 'Character found, so this is no separator Case Else LastWasSeperator = false End Select next i Application.LeaveWait() 'remove Hourglass cursor MsgBox( "The number of selected words is: " & NumFound,_ 0+64, "Werner's WordCount") End Sub