Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-29-2017, 12:04 PM
Hapster Hapster is offline Word Count Macro Windows 10 Word Count Macro Office XP
Novice
Word Count Macro
 
Join Date: Jul 2017
Posts: 1
Hapster is on a distinguished road
Default 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....
Reply With Quote
  #2  
Old 07-29-2017, 08:22 PM
mana mana is offline Word Count Macro Windows 7 64bit Word Count Macro Office 2010 32bit
Novice
 
Join Date: Mar 2017
Posts: 2
mana is on a distinguished road
Default

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
Reply With Quote
  #3  
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,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
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
  #4  
Old 07-30-2017, 03:51 PM
macropod's Avatar
macropod macropod is offline Word Count Macro Windows 7 64bit Word Count Macro Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

See also: https://social.msdn.microsoft.com/Fo...?forum=worddev
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 08-01-2017, 03:30 AM
mana mana is offline Word Count Macro Windows 7 64bit Word Count Macro Office 2010 32bit
Novice
 
Join Date: Mar 2017
Posts: 2
mana is on a distinguished road
Default

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.
Reply With Quote
  #6  
Old 08-01-2017, 07:19 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,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
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 04:01 AM.


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