Guest User

Untitled

a guest
Oct 14th, 2014
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub countWordsSpecial()
  2.     wholetext = ActiveDocument.range(Selection.range.start, Selection.range.End)
  3.    
  4.     While InStr(wholetext, Chr(13))
  5.         wholetext = Replace(wholetext, Chr(13), " ")
  6.     Wend
  7.    
  8.     While InStr(wholetext, "  ")
  9.         wholetext = Replace(wholetext, "  ", " ")
  10.     Wend
  11.    
  12.     Dim arr() As String
  13.     arr() = Split(wholetext, " ")
  14.    
  15.     Dim tmp As String
  16.     Dim wordcount As Long
  17.     braceDepth = 0
  18.     wordcount = 0
  19.     For Each element In arr()
  20.         tmp = element
  21.         If (Not "" = tmp) Then
  22.             If StartsWith(tmp, "[[") Then
  23.                braceDepth = braceDepth + 1
  24.             ElseIf EndsWith(tmp, "]]") Then
  25.                braceDepth = braceDepth - 1
  26.             ElseIf (braceDepth = 0) Then
  27.                 wordcount = wordcount + 1
  28.             End If
  29.         End If
  30.     Next element
  31.  
  32.     MsgBox wordcount
  33. End Sub
  34.  
  35. Public Function EndsWith(str As String, ending As String) As Boolean
  36.      Dim endingLen As Integer
  37.      endingLen = Len(ending)
  38.      EndsWith = (Right(Trim(UCase(str)), endingLen) = UCase(ending))
  39. End Function
  40.  
  41. Public Function StartsWith(str As String, start As String) As Boolean
  42.      Dim startLen As Integer
  43.      startLen = Len(start)
  44.      StartsWith = (Left(Trim(UCase(str)), startLen) = UCase(start))
  45. End Function
Advertisement
Add Comment
Please, Sign In to add comment