#1
|
|||
|
|||
Word Count Macro
Any macro to count EVERY 25 words in a document and put a "/" (slash mark) at every 25 words? Thanks so much....and apologies if this is posted elsewhere. I couldn't find it.
Best.... |
#2
|
|||
|
|||
Code:
Option Explicit Sub test() Dim n As Long With ActiveDocument.Words Do n = n + 26 If n > .Count Then Exit Do .Item(n).InsertBefore "/" Loop End With End Sub |
#3
|
|||
|
|||
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 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 |
#4
|
||||
|
||||
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
gmaxey,
I'm always thanksful for your help. I can understand your code but I still can't write by myself. My word vba has a long way to go. |
#6
|
|||
|
|||
mana,
You seem to have a good handle on dictionaries ;-) If we conclude that a word is something that is delimited in a document by white space then the following may get us closer to a workable solution: Code:
Sub ScratchMacro() 'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 8/1/2017 Dim oWord As Range Dim oRng As Word.Range Dim lngCount As Long Dim lngIndex As Long Dim lngMark As Long lngCount = InputBox("Enter the number of words to count", "Word Count", "25") With ActiveDocument For lngIndex = .Comments.Count To 1 Step -1 If InStr(.Comments(lngIndex).Range.Text, "[Word Count:") > 0 Then .Comments(lngIndex).Delete End If Next Set oRng = ActiveDocument.Range End With lngMark = 0 Do For Each oWord In oRng.Words If oWord.InRange(oRng) Then If Not fcnIsWhiteSpace(oWord) Then lngIndex = lngIndex + 1 Do Until fcnIsWhiteSpace(oWord.Characters.Last) Or fcnIsWhiteSpace(oWord.Characters.Last.Next) oWord.MoveEnd wdWord, 1 oRng.Start = oWord.Duplicate.End Loop Do While fcnIsWhiteSpace(oWord.Characters.Last) Or oWord.Characters.Last Like "[)}:;./?/!" & Chr(148) & "," & Chr(34) & "]" oWord.MoveEnd wdCharacter, -1 Loop Do Until oWord.Characters.Last <> "]" oWord.MoveEnd wdCharacter, -1 Loop If lngIndex = lngCount Then lngMark = lngMark + lngIndex lngIndex = 0 oRng.Comments.Add oWord, "[Word Count: " & lngMark & "]" End If End If End If Next Loop Until oRng.End = ActiveDocument.Range.End 'Here is where we coudld convert the comments to "instring" flags as the OP requested. ' For lngIndex = ActiveDocument.Comments.Count To 1 Step -1 ' If Left(ActiveDocument.Comments(lngIndex).Range.Text, 11) = "[Word Count" Then ' Set oRng = ActiveDocument.Comments(lngIndex).Reference ' oRng.InsertAfter "/" ' ActiveDocument.Comments(lngIndex).Delete ' End If ' Next lbl_Exit: Exit Sub End Sub Private Function fcnIsWhiteSpace(oRngEndCharacter As Range) As Boolean 'If its nothing the it isn't whitespace! fcnIsWhiteSpace = False If Not oRngEndCharacter Is Nothing Then Select Case AscW(oRngEndCharacter.Text) Case 8194, 8195, 8197, 9, 11, 13, 32, 160: fcnIsWhiteSpace = True End Select End If End Function |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
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 |
A recurring word count macro? | bpanda | Word VBA | 1 | 06-11-2013 07:17 AM |
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 |