#1
|
|||
|
|||
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 |
#2
|
||||
|
||||
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 [Fmr MS MVP - Word] |
#3
|
|||
|
|||
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. |
#4
|
||||
|
||||
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
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
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 |
#6
|
||||
|
||||
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 [Fmr MS MVP - Word] |
#7
|
|||
|
|||
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 |
#8
|
||||
|
||||
Indeed. Let's start with you telling use what you're trying to achieve.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#9
|
|||
|
|||
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.
|
#10
|
||||
|
||||
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 [Fmr MS MVP - Word] |
#11
|
|||
|
|||
ok, that's something to experiment with. Many thanks for the help.
Andrew |
|
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 |
Global macro across a number of different word files | daffy | Word VBA | 6 | 07-08-2014 05:42 PM |
Need a Macro that Combines Every 5 sentences into a paragraph | jgarland | Word | 22 | 01-11-2012 11:19 AM |
Page number Macro | kimsi | Word | 3 | 11-15-2011 11:54 PM |