Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-30-2017, 07:45 AM
gmaxey gmaxey is offline Word Count Macro Windows 7 32bit Word Count Macro Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,635
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

Mana,



Word doesn't consider the concept of a "word" the same as you or me or most of the other 6 plus billion souls who use words.

Consider this simple four word paragraph:

Hey! That's my car!

If selected:

Code:
Sub Folly()
Dim lngIndex As Long
  MsgBox Selection.Words.Count
  For lngIndex = 1 To Selection.Words.Count
    MsgBox Selection.Words(lngIndex)
  Next
End Sub
Then there are hyphenated words and on and on. You could probably spend all day and still find conditions to deal with.

Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 7/30/2017
Dim oRng As Range
Dim lngIndex As Long
  Set oRng = ActiveDocument.Range
  oRng.Collapse wdCollapseStart
  Do
    For lngIndex = 1 To 25
      oRng.MoveEnd wdWord, 1
      'Deal with hyhenated words e.g., twenty-one
      On Error GoTo lbl_Skip
      If oRng.Characters.Last.Next = "-" And oRng.Characters.Last.Next.Next Like "[A-Za-z]" Then
        oRng.MoveEnd wdWord, 2
      End If
      'Deal with sentence punctuation.
      If oRng.Characters.Last.Next Like "[.,:;/?/!]" And oRng.Characters.Last.Next.Next Like "[" & Chr(11) & "," & Chr(13) & "]" Then
        oRng.MoveEnd wdWord, 1
      End If
      If oRng.Characters.Last Like " " And oRng.Characters.Last.Previous Like "[.,:;/?/!]" Then
        oRng.MoveEnd wdWord, 1
      End If
lbl_Skip:
      On Error GoTo 0
      If oRng.Text Like Chr(13) Then oRng.MoveEnd wdWord, 1
      If oRng.Text Like Chr(11) Then oRng.MoveEnd wdWord, 1
      If lngIndex = 25 Then
        oRng.Collapse wdCollapseEnd
        oRng.InsertBefore "~*~"
        oRng.Collapse wdCollapseEnd
      End If
      If oRng.End = ActiveDocument.Range.End - 1 Then Exit For
    Next
    If oRng.End = ActiveDocument.Range.End - 1 Then Exit Do
  Loop
  Set oRng = ActiveDocument.Range
  With oRng.Find
    .Text = "~*~"
    .Replacement.Text = "/"
    .Execute Replace:=wdReplaceAll
  End With
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Word Count Macro Macro to return a word count of text contained within a bookmark tomsrv Word VBA 13 08-24-2016 02:26 AM
Ammunition count macro Owastell Excel Programming 3 07-03-2014 11:00 AM
Word Count Macro A recurring word count macro? bpanda Word VBA 1 06-11-2013 07:17 AM
Word Count Macro Word Count Macro bpanda Word VBA 1 01-11-2013 06:51 PM
*Word 2007 Macro for Character Count gbartlet Word 0 07-21-2010 11:12 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:59 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft