Microsoft Office Forums Macro to number sentences

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-15-2017, 01:19 PM
tjf816 tjf816 is offline Macro to number sentences Windows 7 32bit Macro to number sentences Office 2013
Novice
Macro to number sentences
 
Join Date: Feb 2017
Posts: 2
tjf816 is on a distinguished road
Default Macro to number sentences

Hi,


Is it possible to code a macro to find and replace instances of punctuation (i.e., periods, exclamation marks, and question marks) with highlighted text that says "Sentence #X" with X being an auto-generated number?

I'm sorry I can't provide my own attempt at coding it as I have no experience coding. Thank you for your time and patience.

TJ
Reply With Quote
  #2  
Old 02-15-2017, 03:11 PM
macropod's Avatar
macropod macropod is offline Macro to number sentences Windows 7 64bit Macro to number sentences Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 19,670
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

That's not possible with VBA, the reason being that VBA's idea of a sentence is non-grammatical. For example, consider the following:

Mr. Smith spent $1,234.56 at Dr. John's Grocery Store, to buy: 10.25kg of potatoes; 10kg of avocados; and 15.1kg of Mrs. Green's Mt. Pleasant macadamia nuts.

For you and me, that would count as one sentence; for VBA it counts as 5 sentences.
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #3  
Old 02-16-2017, 06:10 AM
tjf816 tjf816 is offline Macro to number sentences Windows 7 32bit Macro to number sentences Office 2013
Novice
Macro to number sentences
 
Join Date: Feb 2017
Posts: 2
tjf816 is on a distinguished road
Default

Hi,

Thank you for your prompt reply. Even if it adds sentence numbers to periods that don't mark the end of clauses, it would still be a great benefit to me. The amount of time it would take to delete the extraneous sentence numbers would be far shorter than it would take to add them in manually.
Reply With Quote
  #4  
Old 02-16-2017, 01:42 PM
macropod's Avatar
macropod macropod is offline Macro to number sentences Windows 7 64bit Macro to number sentences Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 19,670
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Try:
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim i As Long, Rng As Range
With ActiveDocument.Range
  For i = 1 To .Sentences.Count
    With .Sentences(i)
      .InsertBefore " "
      Set Rng = .Characters.First
      Rng.Collapse wdCollapseStart
      .Fields.Add Range:=Rng, Type:=wdFieldSequence, _
        Text:="No.", PreserveFormatting:=False
    End With
  Next
End With
Set Rng = Nothing
Application.ScreenUpdating = True
End Sub
With this code, the numbering is applied via SEQ fields. Had I hard-coded the numbers, you would have had to re-number every sentence after the first one that gets mis-numbered; this way simply deleting the offending numbers then updating the fields (e.g. Ctrl-A, F9) - which you can leave till all the offending numbers have been deleted - is all you need do.
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #5  
Old 03-24-2017, 06:25 PM
andrew626 andrew626 is offline Macro to number sentences Windows 7 64bit Macro to number sentences Office 2013
Novice
 
Join Date: Mar 2017
Posts: 5
andrew626 is on a distinguished road
Default Additional question

Hi everyone
reference this question: Is it possible to search for the paragraph symbol at the end of the sentence rather than a full stop ?

And in general, is it possible to search for tabs, line/page breaks etc?

Many thanks
Andrew
Reply With Quote
  #6  
Old 03-24-2017, 09:29 PM
macropod's Avatar
macropod macropod is offline Macro to number sentences Windows 7 64bit Macro to number sentences Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 19,670
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

You can search for spaces, tabs, paragraph breaks, manual line breaks, manual page breaks. Section breaks, etc. All of these - and more - are listed in Word's Find>Special dropdown.
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #7  
Old 03-26-2017, 11:55 AM
andrew626 andrew626 is offline Macro to number sentences Windows 7 64bit Macro to number sentences Office 2013
Novice
 
Join Date: Mar 2017
Posts: 5
andrew626 is on a distinguished road
Default

Sorry, I should have been more specific. How are the paragraph symbols, tabs etc coded in a macro? (I guess not as ^p, ^t etc).

And if you use the paragraph symbol code, will the VBA macro then treat that selection as sentence?

Thanks
Reply With Quote
  #8  
Old 03-26-2017, 03:58 PM
macropod's Avatar
macropod macropod is offline Macro to number sentences Windows 7 64bit Macro to number sentences Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 19,670
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Quote:
Originally Posted by andrew626 View Post
Sorry, I should have been more specific.
Indeed. Let's start with you telling use what you're trying to achieve.
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #9  
Old 03-27-2017, 02:11 PM
andrew626 andrew626 is offline Macro to number sentences Windows 7 64bit Macro to number sentences Office 2013
Novice
 
Join Date: Mar 2017
Posts: 5
andrew626 is on a distinguished road
Default

Just interested in learning how to use (maybe even write sometime) macros. Usually try to learn by modding something already written to do something else, so when I saw the macro you wrote for tjf, I thought it might be useful to play around with (only a dozen or so lines to understand) and renumber sentences by looking for the . for instance, so as to avoid the . occurring as decimal points etc in your example sentence. So, purely a learning exercise.
Reply With Quote
  #10  
Old 03-27-2017, 04:27 PM
macropod's Avatar
macropod macropod is offline Macro to number sentences Windows 7 64bit Macro to number sentences Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 19,670
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Well, for paragraphs, you could simply replace '.Sentences' in the code in post #4 with '.Paragraphs', but that wouldn't make a lot of sense when Word already has auto-numbering for paragraphs. An alternative approach would be to use a macro-driven Find/Replace which, as I said, can be used for spaces, tabs, paragraph breaks, manual line breaks, manual page breaks. Section breaks, etc., but even that wouldn't be appropriate for paragraph numbering. That said, the Find/Replace code might be structured along the lines of the macro here: https://www.msofficeforums.com/word-...tml#post112574
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #11  
Old 03-29-2017, 05:42 PM
andrew626 andrew626 is offline Macro to number sentences Windows 7 64bit Macro to number sentences Office 2013
Novice
 
Join Date: Mar 2017
Posts: 5
andrew626 is on a distinguished road
Default

ok, that's something to experiment with. Many thanks for the help.

Andrew
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Get paragraph number from macro brice Word VBA 4 06-30-2015 03:29 AM
How to find number of coma and then add that number of rows in word using macro? PRA007 Word VBA 7 05-27-2015 10:45 PM
Macro to number sentences Global macro across a number of different word files daffy Word VBA 6 07-08-2014 05:42 PM
Macro to number sentences Need a Macro that Combines Every 5 sentences into a paragraph jgarland Word 22 01-11-2012 11:19 AM
Macro to number sentences Page number Macro kimsi Word 3 11-15-2011 11:54 PM


All times are GMT -7. The time now is 07:53 AM.


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