Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-27-2015, 12:11 PM
gn4619 gn4619 is offline How to select the text of certain length? Windows 7 64bit How to select the text of certain length? Office 2010 32bit
Novice
How to select the text of certain length?
 
Join Date: Apr 2015
Posts: 25
gn4619 is on a distinguished road
Default How to select the text of certain length?


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!
Reply With Quote
  #2  
Old 04-27-2015, 06:26 PM
Guessed's Avatar
Guessed Guessed is offline How to select the text of certain length? Windows 7 32bit How to select the text of certain length? Office 2010 32bit
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,158
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #3  
Old 04-28-2015, 06:44 AM
gn4619 gn4619 is offline How to select the text of certain length? Windows 7 64bit How to select the text of certain length? Office 2010 32bit
Novice
How to select the text of certain length?
 
Join Date: Apr 2015
Posts: 25
gn4619 is on a distinguished road
Default Thanks.

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.
Reply With Quote
  #4  
Old 04-28-2015, 10:28 PM
Guessed's Avatar
Guessed Guessed is offline How to select the text of certain length? Windows 7 32bit How to select the text of certain length? Office 2010 32bit
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,158
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #5  
Old 04-29-2015, 05:57 AM
gmaxey gmaxey is offline How to select the text of certain length? Windows 7 32bit How to select the text of certain length? Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,598
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #6  
Old 04-29-2015, 04:22 PM
gn4619 gn4619 is offline How to select the text of certain length? Windows 7 64bit How to select the text of certain length? Office 2010 32bit
Novice
How to select the text of certain length?
 
Join Date: Apr 2015
Posts: 25
gn4619 is on a distinguished road
Default Thanks! Your code works for me.

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!
Reply With Quote
  #7  
Old 04-30-2015, 12:56 PM
gn4619 gn4619 is offline How to select the text of certain length? Windows 7 64bit How to select the text of certain length? Office 2010 32bit
Novice
How to select the text of certain length?
 
Join Date: Apr 2015
Posts: 25
gn4619 is on a distinguished road
Default I met one issue.

When the cursor moves into a textbox, it will stop there.
Reply With Quote
Reply



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
How to select the text of certain length? 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
How to select the text of certain length? Import from variable length text diouna Excel 3 07-10-2012 09:43 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:56 AM.


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