|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
Help Writing more Customized Command to Select Next Sentence in Microsoft Word
I am very new to VBA coding but am realizing how powerful it is. To this point I have been using Autohotkey to automate Word, but it cannot handle one task I have in mind:
I want a macro that moves the insertion point to the beginning of the next sentence. Obviously the one built in to Word already is as follows: Code:
Selection.MoveRight Unit:=wdSentence, Count:=1 So the only thing I want to change about Word’s definition of a sentence is to exclude certain words from its definition that I know are not the end of a sentence, like "Mr. " and the examples I give above. But writing this macro is proving elusive to me despite hours of trying to figure it out. If any coding geniuses could whip up the perfect macro, that would be amazing. If not, could you help me with bits of it? From my research, I am thinking I will have to define a Range that extends from the current insertion point position to the end of the next period plus space or the next new-paragraph sign, but not including instances such as “Dr. ” or “Mr. ” or “Mrs. ” Then I will move the insertion point from its current position to the end of that Range. Also, from my understanding the only way to move the insertion point based on surrounding text is by using the "With Selection.Find" function, as described here. Is that true? This has a good example of how to exclude certain characters from a search using the Find function, but again, I am at a loss to write this complex code with my current knowledge. Thank you so much to anyone willing to help! |
#2
|
||||
|
||||
Word's VBA 'sentence' definition has no idea what a grammatical sentence is, therefore you can't use:
Selection.MoveRight Unit:=wdSentence, Count:=1 reliably to get the next grammatical sentence. The problem with trying to build a set of exclusions of the kind you envisage is that there is no end to the number of possible exceptions once you start trying to cope with every kind of abbreviation & acronym while at the same time handling the fact it's possible a grammatical sentence might actually end with such an abbreviation or acronym. Plus, sentences don't always end with periods... And no, the "With Selection.Find" function isn't the only one; neither would it be my first choice.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Figured out a piece of the puzzle. The following extends the current selection to the next instance of ". ":
Code:
Selection.Extend Selection.Find.ClearFormatting With Selection.Find .Text = ". " .Replacement.Text = "" .Forward = True .Wrap = wdFindAsk End With Selection.Find.Execute |
#4
|
|||
|
|||
What would be your first choice (as opposed to the "With Selection.Find" function) to moving the insertion point based on surrounding text?
|
#5
|
|||
|
|||
Quote:
I don't want to give up yet just because there are other possible acronyms either. The three I mentioned are really the only ones that I encounter in my reading. |
#6
|
||||
|
||||
I'd use something like:
Code:
Sub Demo() Dim strAbbr, i As Long strAbbr = ".Mr.Mrs.etc." With Selection If .Start > .Sentences.First.Start Then .MoveStart wdSentence, -1 If .End < .Sentences.First.End Then .MoveEnd wdSentence, 1 While InStr(strAbbr, "." & Trim(.Words.Last.Previous.Words.First) & ".") > 0 .MoveEnd wdSentence, 1 Wend End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
You might find the following if not helpful then interesting:
http://gregmaxey.com/word_tip_pages/...sentences.html |
#8
|
|||
|
|||
Thank you so much macropod for this code, it is so close to perfect for me! I took out one line because it was starting the selection at the beginning of the sentence that the insertion point is on, whereas I want the selection to start at where the insertion point currently is (even if it is in the middle of the sentence). So now it looks like this:
Code:
Sub Demo() Dim strAbbr, i As Long strAbbr = ".Mr.Mrs.etc." With Selection If .End < .Sentences.First.End Then .MoveEnd wdSentence, 1 While InStr(strAbbr, "." & Trim(.Words.Last.Previous.Words.First) & ".") > 0 .MoveEnd wdSentence, 1 Wend End With End Sub (1) First, and most importantly, if I run this macro while the insetion point is somewhere in the first sentence of a paragraph, so that the rest of the first sentence is selected, and then I run the macro a second time without doing anything else in between, the selection does not extend to the next (second) sentence in the paragraph. This is unlike the normal sentence-extend command (Selection.MoveRight Unit:=wdSentence, Count:=1, Extend:=wdExtend) which will extend the selection to the next sentence each time the command is run. (2) Second, if I run this macro while the insetion point is in a ONE-sentence paragraph and the next line directly below this one-sentence paragraph has no text (i.e., it’s just a blank line with a Paragraph sign), the selection will extend down to also include the Paragraph sign of this second line. It would ideally stop after the first line and not select anything on the second line below. Strangely, the selection does stop immediately after the first sentence of a one-sentence paragraph if the very next line directly below does have some text. If you could help me iron out these two issues, I would be infinitely grateful. I am even willing to pay someone to do this, it is that important to me to find an answer. I have tried understanding what needs to be changed, but it is over my head, as I couldn’t find documentation on the internet for some of the phrases your code uses. And thank you gmaxey for your suggestion. I loaded that Add-in, but unfortunately it only selects entire sentences, and I can’t examine its coding. But I am very glad to see that I am not the only one who wants to do something like this! |
#9
|
|||
|
|||
Code:
Sub ScratchMacro() 'A basic Word macro coded by Greg Maxey Dim strAbbr, i As Long Dim oRng As Range strAbbr = ".Mr.Mrs.etc." With Selection Set oRng = Selection.Range .Collapse 0 If .End <= .Sentences.First.End Then .MoveEnd wdSentence, 1 Do While InStr(strAbbr, "." & Trim(.Words.Last.Previous.Words.First) & ".") > 0 Or Selection.Text = vbCr .MoveEnd wdSentence, 1 Loop oRng.End = .End End With If oRng.Characters.Last.Previous = vbCr Then oRng.MoveEnd wdCharacter, -1 oRng.Select If Selection.Characters.Last.Previous = vbCr Then Selection.Move wdCharacter, -1 lbl_Exit: Exit Sub End Sub |
#10
|
|||
|
|||
Thank you so much for your help gmaxey, this code is genius! I just realized that you are the creator of that great add-in for selecting sentences that you recommended, so clearly I came to the right person.
I only have one complaint with the code now, which is that if the insertion point is somewhere in the last sentence of a paragraph, and that paragraph is followed by two or more blank lines without text, executing the code will do nothing. The insertion point will stay exactly where it is. Instead, it ideally should still select to the end of that last sentence in that Paragraph, as it would do if there happened to be text in the new Paragraph directly below it, or as it would do if there only were one blank line below it. Any ideas on a fix for this? |
#11
|
|||
|
|||
I will look at it more tonight when I get home. However, the best option is NOT to use empty paragraphs for layout. That is the purpose of paragraph space before or after.
|
#12
|
|||
|
|||
Here is another adaptation. Notice this also addresses the issue of "., " tripping things up e.g., when you use e.g., in a sentence:
Code:
Sub ScratchMacro() 'A basic Word macro coded by Greg Maxey Dim strAbbr, i As Long Dim oRng As Range strAbbr = ".Mr.Mrs.etc." With Selection Set oRng = Selection.Range .Collapse 0 If .End <= .Sentences.First.End Then .MoveEnd wdSentence, 1 Do While InStr(strAbbr, "." & Trim(.Words.Last.Previous.Words.First) & ".") > 0 Or Selection.Text = vbCr .MoveEnd wdSentence, 1 Loop Do While Right(.Text, 3) = "., " .MoveEnd wdSentence, 1 Loop oRng.End = .End End With Do While oRng.Characters.Last = vbCr Or oRng.Characters.Last = Chr(11) oRng.MoveEnd wdCharacter, -1 Loop oRng.Select lbl_Exit: Exit Sub End Sub |
#13
|
|||
|
|||
I really cannot thank you enough for your help gmaxey. That one is virtually perfect!
I fooled around with another method of accomplishing my goal: Code:
Selection.MoveRight Unit:=wdSentence, Count:=1, extend:=wdExtend Do While Strings.Right(Selection.Range.Text, 4) = "Mr. " Or Strings.Right(Selection.Range.Text, 5) = "Mrs. " Or Strings.Right(Selection.Range.Text, 1) = vbCr Selection.MoveRight Unit:=wdSentence, Count:=1, extend:=wdExtend Loop End Sub Again, thank you gmaxey and macropod! |
#14
|
|||
|
|||
Try this:
Code:
Sub ScratchMacro() 'A basic Word macro coded by Greg Maxey Dim strAbbr, i As Long Dim oRng As Range strAbbr = ".Mr.Mrs.Ms.etc." With Selection Set oRng = Selection.Range On Error GoTo Err_End Do While .Characters.Last.Next = vbCr .MoveEnd wdCharacter, 1 Loop Set oRng = Selection.Range .Collapse 0 If .End <= .Sentences.First.End Then .MoveEnd wdSentence, 1 Do While InStr(strAbbr, "." & Trim(.Words.Last.Previous.Words.First) & ".") > 0 _ Or Selection.Text = vbCr _ Or Right(.Text, 3) = "., " .MoveEnd wdSentence, 1 Loop oRng.End = .End End With Do While oRng.Characters.Last = vbCr Or oRng.Characters.Last = Chr(11) oRng.MoveEnd wdCharacter, -1 Loop oRng.Select lbl_Exit: Exit Sub Err_End: Resume lbl_Exit End Sub |
#15
|
|||
|
|||
That is perfect, thank you! Such a great code!
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Select backstage Print command programmatically | NobodysPerfect | Excel Programming | 0 | 04-23-2015 08:52 AM |
Select the whole sentence when a word has been modified | gloos | Word VBA | 1 | 04-14-2015 10:24 PM |
VBA code for Microsoft Word macro — select text and insert footnote | ndnd | Word VBA | 10 | 01-06-2015 01:47 PM |
Writing fractions using the Mathematics programme of Microsoft Word 2010 | Microsoftenquirer1000 | Word | 8 | 11-19-2014 02:10 PM |
Microsoft Word 2003: casual style from the writing box? | intooutfrom | Word | 0 | 01-04-2010 01:52 PM |