![]() |
|
|
|
#1
|
|||
|
|||
|
In a document, I want to set a starting point, and define a total of word count (such as 1,000 words), and let the cursor move to the point where the text in between has that many words. Any help would be appreciated! |
|
#2
|
||||
|
||||
|
Microsoft's definition of what is a 'word' apparently includes punctuation but this might get you started
Code:
Sub ExpandWords()
Dim x As Integer
x = InputBox("How many words?", "Counter", 10)
If x > 0 Then
Selection.MoveRight Unit:=wdWord, Count:=x, Extend:=True
End If
End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
|
#3
|
|||
|
|||
|
Thanks.
I thought of the same method, but as you said, this method counted words and punctuation marks. In Word 2010, the word count counts only real words. What I need is to have the cursor move from a certain point to cover a certain number of real words. |
|
#4
|
||||
|
||||
|
Your profile says you are using Word 2010.
You could modify the macro to expand by a word for each punctuation mark but it might need to be version specific if Word 2010 can count words better than 2013.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
|
#5
|
|||
|
|||
|
You might need to tweak it a bit, but try this:
Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Word.Range
Dim lngCount As Long, lngCounted As Long
lngCount = InputBox("Enter the number of words to move", "Move Cursor X Words", 10)
Set oRng = Selection.Range
On Error GoTo Err_Handler
Do
Debug.Print oRng.Words.Last.Next
If Len(oRng.Words.Last.Next) > 1 Then
oRng.Move wdWord, 1
Else
If oRng.Words.Last.Next Like "[.,;:\!]" Then
If lngCounted = lngCount - 1 Then
oRng.Move wdWord, 1
Else
oRng.Move wdWord, 2
End If
Else
oRng.Move wdWord, 1
End If
End If
lngCounted = lngCounted + 1
Loop Until lngCounted = lngCount
lbl_Exit:
oRng.Select
Exit Sub
Err_Handler:
MsgBox "There is not that many words!"
Resume lbl_Exit
End Sub
|
|
#6
|
|||
|
|||
|
Thank you very much, Greg. You code counts the real words, just like the built-in word count of both Word 2010 and 2013.
I adapted a little bit by using a userform and prompting if the words entered are more than there are between the cursor point and end of document. Thanks! |
|
#7
|
|||
|
|||
|
When the cursor moves into a textbox, it will stop there.
|
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| VBA Search Table for Text/Select Text/Insert Hyperlink | sldrellich | Word VBA | 3 | 03-24-2015 01:09 PM |
| Microsoft Word macro to find text, select all text between brackets, and delete | helal1990 | Word VBA | 4 | 02-05-2015 03:52 PM |
Select section of text and change text
|
newbieX | Word VBA | 3 | 03-28-2014 04:21 PM |
| Limit text length Content Control | NobodysPerfect | Word | 10 | 02-28-2014 02:40 PM |
Import from variable length text
|
diouna | Excel | 3 | 07-10-2012 09:43 PM |