Advertisement
Guest User

Untitled

a guest
Mar 1st, 2015
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. ABZBB
  2.  
  3. A x 1
  4. B x 3
  5. Z x 1
  6.  
  7. Sub Macro1()
  8. Dim Box As Shape
  9. Set Box = ActiveDocument.Shapes.AddTextbox( _
  10. Orientation:=msoTextOrientationHorizontal, _
  11. Left:=50, Top:=50, Width:=200, Height:=400)
  12. Box.TextFrame.TextRange.Text = "My text comes this way" + Chr(10)
  13. Dim s As String
  14. Application.ScreenUpdating = False
  15. docLength = ActiveDocument.Range.Characters.Count
  16.  
  17. Box.TextFrame.TextRange.Text = Box.TextFrame.TextRange.Text + "Text length is: " + Str(docLength) + Chr(10)
  18.  
  19. Dim arr(128) As Integer
  20. Dim character As Integer
  21. For i = 1 To docLength - 1
  22.  
  23. character = Asc(ActiveDocument.Range.Characters(i))
  24. If iAsc >= 0 And iAsc <= 127 Then
  25. arr(character) = arr(character) + 1
  26. End If
  27. Next i
  28.  
  29.  
  30. End Sub
  31.  
  32. Sub CountChars()
  33. Dim iCount(57) As Integer
  34. Dim x As Integer
  35. Dim iTotal As Integer
  36. Dim iAsc As Integer
  37.  
  38. Application.ScreenUpdating = False
  39. iTotal = ActiveDocument.Range.Characters.Count
  40.  
  41. For x = 1 To iTotal
  42. iAsc = Asc(ActiveDocument.Range.Characters(x))
  43. If iAsc >= 65 And iAsc <= 122 Then
  44. iCount(iAsc - 65) = iCount(iAsc - 65) + 1
  45. End If
  46. Next x
  47. For x = 0 To 57
  48. Debug.Print x, iCount(x)
  49. Next x
  50. Application.ScreenUpdating = True
  51. End Sub
  52.  
  53. Debug.Print Chr(x + 65), iCount(x)
  54.  
  55. 'Tools, References: Microsoft Scripting Runtime
  56. Sub CountCharsWithReplace()
  57. Dim doc As Document
  58. Dim rDupe As Range
  59. Dim dicChars As Scripting.Dictionary
  60. Dim s As String
  61. Dim iTotalChars As Integer
  62. Dim iTempChars As Integer
  63. Dim iDiff As Integer
  64. Dim n As Integer
  65. Dim blnExec As Boolean
  66. Dim lett As Variant
  67. Application.ScreenUpdating = False
  68. Set doc = ActiveDocument
  69. iTotalChars = doc.Range.Characters.Count
  70. Set rDupe = doc.Range
  71. Set dicChars = New Scripting.Dictionary
  72. Do While rDupe.Characters.Count > 1
  73. s = rDupe.Characters(1).Text
  74. blnExec = rDupe.Find.Execute(s, , , , , , , , , "", wdReplaceAll)
  75. iTempChars = doc.Range.Characters.Count
  76. iDiff = iTotalChars - iTempChars
  77. iTotalChars = iTempChars
  78. If Asc(s) >= 65 And Asc(s) <= 122 Then
  79. dicChars.Add s, iDiff
  80. End If
  81. n = n + 1
  82. Loop
  83. ActiveDocument.Undo Times:=n
  84. Application.ScreenUpdating = True
  85. For Each lett In dicChars.Keys
  86. Debug.Print lett, dicChars(lett)
  87. Next lett
  88. End Sub
  89.  
  90. ActiveDocument.Range.ComputeStatistics(wdStatisticCharacters)
  91.  
  92. Activedocument.Range.Characters.Count
  93.  
  94. Selection.Range.ComputeStatistics(wdStatisticCharacters)
  95.  
  96. Selection.Range.Characters.Count
  97.  
  98. Function GetCharCountLoop(doc As Word.Document, char As String) As Long
  99. Dim i As Long
  100. Dim CharCount As Long
  101.  
  102. With doc.Content.Characters
  103. For i = 1 To .Count
  104. If .Item(i) = char Then
  105. CharCount = CharCount + 1
  106. End If
  107. Next i
  108. End With
  109. GetCharCountLoop = CharCount
  110. End Function
  111.  
  112. Function GetCharCountFind(doc As Word.Document, char As String) As Long
  113. Dim i As Long
  114. Dim CharCount As Long
  115.  
  116. With doc.Content.Find
  117. Do While .Execute(FindText:=char, Forward:=True, MatchWholeWord:=False, MatchCase:=True) = True
  118. CharCount = CharCount + 1
  119. Loop
  120. GetCharCountFind = CharCount
  121. End With
  122. End Function
  123.  
  124. Function GetCharCountString(doc As Word.Document, char As String) As Long
  125. Dim chars As String
  126. Dim i As Long
  127. Dim CharCount As Long
  128.  
  129. chars = doc.Content
  130. For i = 1 To Len(chars)
  131. If Mid$(chars, i, 1) = char Then
  132. CharCount = CharCount + 1
  133. End If
  134. Next i
  135. GetCharCountString = CharCount
  136. End Function
  137.  
  138. Function GetCharCountRegex(doc As Word.Document, char As String) As Long
  139. Dim chars As String
  140. Dim CharCount As Long
  141. Dim objRegExp As Object
  142.  
  143. chars = doc.Content
  144. Set objRegExp = CreateObject("VBScript.RegExp")
  145. With objRegExp
  146. .Pattern = char
  147. .IgnoreCase = False
  148. .Global = True
  149. CharCount = .Execute(chars).Count
  150. End With
  151. GetCharCountRegex = CharCount
  152. End Function
  153.  
  154. Sub TimeMethods()
  155. Dim char As String
  156. Dim CharCount As Long
  157. Dim LoopCounter As Long
  158. Dim NumLoops As Long
  159. Dim StartTime As Double
  160.  
  161. char = "a"
  162. NumLoops = 1
  163.  
  164. StartTime = Timer
  165. For LoopCounter = 1 To NumLoops
  166. CharCount = GetCharCountLoop(ActiveDocument, char)
  167. Next LoopCounter
  168. Debug.Print CharCount
  169. Debug.Print Timer - StartTime
  170.  
  171. StartTime = Timer
  172. For LoopCounter = 1 To NumLoops
  173. CharCount = GetCharCountFind(ActiveDocument, char)
  174. Next LoopCounter
  175. Debug.Print CharCount
  176. Debug.Print Timer - StartTime
  177.  
  178. StartTime = Timer
  179. For LoopCounter = 1 To NumLoops
  180. CharCount = GetCharCountString(ActiveDocument, char)
  181. Next LoopCounter
  182. Debug.Print CharCount
  183. Debug.Print Timer - StartTime
  184.  
  185. StartTime = Timer
  186. For LoopCounter = 1 To NumLoops
  187. CharCount = GetCharCountRegex(ActiveDocument, char)
  188. Next LoopCounter
  189. Debug.Print CharCount
  190. Debug.Print Timer - StartTime
  191.  
  192. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement