Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-06-2016, 10:26 PM
poetofpiano poetofpiano is offline Help Writing more Customized Command to Select Next Sentence in Microsoft Word Windows 8 Help Writing more Customized Command to Select Next Sentence in Microsoft Word Office 2013
Novice
Help Writing more Customized Command to Select Next Sentence in Microsoft Word
 
Join Date: Sep 2015
Posts: 25
poetofpiano is on a distinguished road
Default 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
But the thing I don’t like about this one is that it thinks a word like “Dr. ” or “Mr. ” or “Mrs. ” is the end of a sentence when in fact it is not. The spaces following the periods in my quotes are not an accident, because Word defines the end of a sentence as (1) a period plus a space or (2) simply a Paragraph sign (no period necessary for this second case). Thus, “A.a” would not be considered to be the end of a sentence because there is no space after the period.

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!
Reply With Quote
  #2  
Old 03-06-2016, 11:43 PM
macropod's Avatar
macropod macropod is offline Help Writing more Customized Command to Select Next Sentence in Microsoft Word Windows 7 64bit Help Writing more Customized Command to Select Next Sentence in Microsoft Word Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #3  
Old 03-07-2016, 12:06 AM
poetofpiano poetofpiano is offline Help Writing more Customized Command to Select Next Sentence in Microsoft Word Windows 8 Help Writing more Customized Command to Select Next Sentence in Microsoft Word Office 2013
Novice
Help Writing more Customized Command to Select Next Sentence in Microsoft Word
 
Join Date: Sep 2015
Posts: 25
poetofpiano is on a distinguished road
Default

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
Now I just need to add exclusions and also include the paragraph symbol as a match for the search, and then just put it all together. Again, thanks again to anyone who can help!
Reply With Quote
  #4  
Old 03-07-2016, 12:09 AM
poetofpiano poetofpiano is offline Help Writing more Customized Command to Select Next Sentence in Microsoft Word Windows 8 Help Writing more Customized Command to Select Next Sentence in Microsoft Word Office 2013
Novice
Help Writing more Customized Command to Select Next Sentence in Microsoft Word
 
Join Date: Sep 2015
Posts: 25
poetofpiano is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
And no, the "With Selection.Find" function isn't the only one; neither would it be my first choice.
What would be your first choice (as opposed to the "With Selection.Find" function) to moving the insertion point based on surrounding text?
Reply With Quote
  #5  
Old 03-07-2016, 12:22 AM
poetofpiano poetofpiano is offline Help Writing more Customized Command to Select Next Sentence in Microsoft Word Windows 8 Help Writing more Customized Command to Select Next Sentence in Microsoft Word Office 2013
Novice
Help Writing more Customized Command to Select Next Sentence in Microsoft Word
 
Join Date: Sep 2015
Posts: 25
poetofpiano is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
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...
I certainly understand sentences don't always end in a period. That's why I said I want to keep the definition of a sentence for this macro as the distance from the insertion point to the next (1) period plus a space or (2) Paragraph sign.

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.
Reply With Quote
  #6  
Old 03-07-2016, 12:37 AM
macropod's Avatar
macropod macropod is offline Help Writing more Customized Command to Select Next Sentence in Microsoft Word Windows 7 64bit Help Writing more Customized Command to Select Next Sentence in Microsoft Word Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #7  
Old 03-07-2016, 05:22 AM
gmaxey gmaxey is offline Help Writing more Customized Command to Select Next Sentence in Microsoft Word Windows 7 32bit Help Writing more Customized Command to Select Next Sentence in Microsoft Word Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

You might find the following if not helpful then interesting:

http://gregmaxey.com/word_tip_pages/...sentences.html
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #8  
Old 03-07-2016, 04:12 PM
poetofpiano poetofpiano is offline Help Writing more Customized Command to Select Next Sentence in Microsoft Word Windows 8 Help Writing more Customized Command to Select Next Sentence in Microsoft Word Office 2013
Novice
Help Writing more Customized Command to Select Next Sentence in Microsoft Word
 
Join Date: Sep 2015
Posts: 25
poetofpiano is on a distinguished road
Default

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
There are only two problems with it now:

(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!
Reply With Quote
  #9  
Old 03-07-2016, 05:11 PM
gmaxey gmaxey is offline Help Writing more Customized Command to Select Next Sentence in Microsoft Word Windows 7 32bit Help Writing more Customized Command to Select Next Sentence in Microsoft Word Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #10  
Old 03-08-2016, 12:10 AM
poetofpiano poetofpiano is offline Help Writing more Customized Command to Select Next Sentence in Microsoft Word Windows 8 Help Writing more Customized Command to Select Next Sentence in Microsoft Word Office 2013
Novice
Help Writing more Customized Command to Select Next Sentence in Microsoft Word
 
Join Date: Sep 2015
Posts: 25
poetofpiano is on a distinguished road
Default

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?
Reply With Quote
  #11  
Old 03-08-2016, 05:36 AM
gmaxey gmaxey is offline Help Writing more Customized Command to Select Next Sentence in Microsoft Word Windows 7 32bit Help Writing more Customized Command to Select Next Sentence in Microsoft Word Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #12  
Old 03-08-2016, 06:45 PM
gmaxey gmaxey is offline Help Writing more Customized Command to Select Next Sentence in Microsoft Word Windows 7 32bit Help Writing more Customized Command to Select Next Sentence in Microsoft Word Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #13  
Old 03-09-2016, 01:26 PM
poetofpiano poetofpiano is offline Help Writing more Customized Command to Select Next Sentence in Microsoft Word Windows 8 Help Writing more Customized Command to Select Next Sentence in Microsoft Word Office 2013
Novice
Help Writing more Customized Command to Select Next Sentence in Microsoft Word
 
Join Date: Sep 2015
Posts: 25
poetofpiano is on a distinguished road
Default

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
It is not nearly as elegant as yours, and adding new abbreviations to be excluded is much more cumbersome than in yours, so if you have any suggestions I would gladly use them. It functions exactly the same as yours except that with yours, if there is one or more blank lines below the last sentence of a Paragraph, it will not select any further than that last sentence, whereas mine will continue to select the blank lines below one at a time. Not a huge difference at all — I can’t think of a time where I wanted to select blank lines below a paragraph.

Again, thank you gmaxey and macropod!
Reply With Quote
  #14  
Old 03-09-2016, 01:53 PM
gmaxey gmaxey is offline Help Writing more Customized Command to Select Next Sentence in Microsoft Word Windows 7 32bit Help Writing more Customized Command to Select Next Sentence in Microsoft Word Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #15  
Old 03-09-2016, 11:42 PM
poetofpiano poetofpiano is offline Help Writing more Customized Command to Select Next Sentence in Microsoft Word Windows 8 Help Writing more Customized Command to Select Next Sentence in Microsoft Word Office 2013
Novice
Help Writing more Customized Command to Select Next Sentence in Microsoft Word
 
Join Date: Sep 2015
Posts: 25
poetofpiano is on a distinguished road
Default

That is perfect, thank you! Such a great code!
Reply With Quote
Reply



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
Help Writing more Customized Command to Select Next Sentence in Microsoft Word VBA code for Microsoft Word macro — select text and insert footnote ndnd Word VBA 10 01-06-2015 01:47 PM
Help Writing more Customized Command to Select Next Sentence in Microsoft Word 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

Other Forums: Access Forums

All times are GMT -7. The time now is 08:49 AM.


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