Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-22-2023, 10:58 AM
RobiNew RobiNew is offline Getting the last of three names/words Windows 10 Getting the last of three names/words Office 2016
Competent Performer
Getting the last of three names/words
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default Getting the last of three names/words

I'm trying to get the last name in the first paragraph of a doc, but the code here below fails. The string is of this type: John R. Mate or John Rob Mate. Can someone help? Thanks!
Code:
Sub LastName()
Set oRng = ActiveDocument.Range
oRng.Collapse
oRng.MoveEndUntil Cset:=Chr(13)
oRng.Collapse wdCollapseEnd
oRng.Previous(Unit:=wdWord, Count:=1).Select
oRng.Select
'MsgBox oRng.Text
Sub End

Reply With Quote
  #2  
Old 12-22-2023, 03:45 PM
vivka vivka is offline Getting the last of three names/words Windows 7 64bit Getting the last of three names/words Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Hi, RobiNew, your macro with my minor modifications is good :
Code:
Sub LastName()
Set orng = ActiveDocument.range
orng.Collapse
orng.MoveStartUntil Cset:=Chr(13)
orng.Collapse wdCollapseEnd
orng.Previous(Unit:=wdWord, count:=2).Select
MsgBox selection
End Sub
The code works without problems if the first paragraph's last lettered word is followed only by a period, any number (incl. zero) of spaces and VbCr.
Reply With Quote
  #3  
Old 12-22-2023, 04:06 PM
vivka vivka is offline Getting the last of three names/words Windows 7 64bit Getting the last of three names/words Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Also, you can try the following:
Code:
Sub LastName_2()
    ActiveDocument.range.Paragraphs(1).range.words.Last.Select
    selection.Previous(unit:=wdWord, count:=2).Select
   MsgBox selection
  End Sub
Note: instead of 'words' you can use 'Characters' in the 2nd line, which selects the unprintable paragraph sign.


Besides, you can try this one-liner:
Code:
Sub LastName_2()
    ActiveDocument.Paragraphs(1).range.words.Last.Previous(unit:=wdWord, count:=2).Select
End Sub
Reply With Quote
  #4  
Old 12-23-2023, 11:30 AM
RobiNew RobiNew is offline Getting the last of three names/words Windows 10 Getting the last of three names/words Office 2016
Competent Performer
Getting the last of three names/words
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Thank you very much indeed, Vivka! You're always the best. I'll be back to you as soon as possible. Season's Greetings from RobiNew.
Reply With Quote
  #5  
Old 12-23-2023, 11:42 AM
vivka vivka is offline Getting the last of three names/words Windows 7 64bit Getting the last of three names/words Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Thank you, Robinew, for your kind words! I have good teachers here and I'm still learning. Season's greetings!
Reply With Quote
  #6  
Old 12-24-2023, 02:31 AM
RobiNew RobiNew is offline Getting the last of three names/words Windows 10 Getting the last of three names/words Office 2016
Competent Performer
Getting the last of three names/words
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Hi, Vivka! To get the last name in the first paragraph I have adopted your suggestion: ActiveDocument.Paragraphs(1).Range.Words.Last.Prev ious(unit:=wdWord, Count:=1).Select
For the last name before a comma in the first paragraph I have devised the code here below. Is there a more efficient method to achieve the same result? Thanks!
Code:
ActiveDocument.Paragraphs(1).Range.Select
Dim myVar As String
myVar = Selection
If InStr(1, myVar, ",") > 0 Then
Set oRng = ActiveDocument.Range
oRng.Collapse
oRng.MoveStartUntil Cset:=","
oRng.Collapse wdCollapseEnd
oRng.Previous(unit:=wdWord, Count:=1).Select
Reply With Quote
  #7  
Old 12-24-2023, 03:49 AM
vivka vivka is offline Getting the last of three names/words Windows 7 64bit Getting the last of three names/words Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Hi, RobiNew! What if there are many words that start with a capital letter and are followed by a comma in a paragraph? Which one should be selected? If the wrd to select is near the paragraph's start or end, it will be possible to find the 1st from the paragraph's start or end instance of a comma & do the rest of the job.
The code below (it's your's but slightly shortened) finds the 1st occurrence of a comma in the 1st paragraph & selects ANY wd (not only a caplitalized wd) before it. It will be OK if you are sure there's a last name before the 1st occurrence of a comma:
Code:
Sub Find_Select()
ActiveDocument.Paragraphs(1).range.Select
   If InStr(1, selection, ",") > 0 Then
      selection.Collapse
      selection.MoveStartUntil Cset:=","
      selection.Previous(unit:=wdWord, count:=1).Select
   End If
End Sub
Reply With Quote
  #8  
Old 12-24-2023, 04:31 AM
gmayor's Avatar
gmayor gmayor is offline Getting the last of three names/words Windows 10 Getting the last of three names/words Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

A solution to your request depends on several issues which are not known. The first part is simple enough, provided the names are all of similat construction as you described. The second part assumes that there would only be one comma in the paragraph and that is directly after the name you want to find. If there are additional commas the code below will not work.
The code writes to two strings, here shown in a text box. If there is no comma in the paragraph, the second string has no content.

Code:
Sub Macro1()
Dim oRng As Range
Dim sName1 As String, sName2 As String
    Set oRng = ActiveDocument.Paragraphs(1).Range
    With oRng
        'get last name of first paragraph
        'If the last character is punctuation
        Do While Len(.Words.Last) = 1
            .End = .End - 1
        Loop
        .Collapse wdCollapseEnd
        .MoveStart wdWord, -3
        'correct for middle initial
        If Len(.Words(1)) = 1 Then .MoveStart wdWord, -1
        sName1 = oRng.Text
        
        'get last name before comma
        .Start = ActiveDocument.Paragraphs(1).Range.Start
        .End = InStr(1, .Text, Chr(44))
        If .Words.Count > 3 Then
            .End = .End - 1
            .Collapse wdCollapseEnd
            .MoveStart wdWord, -3
             'correct for middle initial
            If InStr(1, .Text, Chr(46)) Then .MoveStart wdWord, -1
            sName2 = oRng.Text
        End If
        MsgBox sName1 & vbCr & sName2
    End With
    Set oRng = Nothing
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #9  
Old 12-24-2023, 07:29 AM
RobiNew RobiNew is offline Getting the last of three names/words Windows 10 Getting the last of three names/words Office 2016
Competent Performer
Getting the last of three names/words
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Thank you, Gmayor! I'll keep your code for more complex strings. Actually my case is not so complex, so I've rewritten my code as follows (thank you, Vivka!!):
Code:
Sub LastNameRN()
Set oRng = ActiveDocument.Paragraphs(1).Range
   If InStr(1, oRng, ",") > 0 Then
      oRng.Collapse
      oRng.MoveStartUntil Cset:=","
      oRng.Previous(unit:=wdWord, Count:=1).Select
   Else
      oRng.Words.Last.Previous(unit:=wdWord, Count:=1).Select
   End If
End Sub
Reply With Quote
  #10  
Old 12-24-2023, 07:57 AM
vivka vivka is offline Getting the last of three names/words Windows 7 64bit Getting the last of three names/words Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

RobiNew, the line oRng.Collapse can be deleted. I wanted to correct my previous code but it was not allowed by the forum's program.
Reply With Quote
  #11  
Old 12-24-2023, 09:34 AM
RobiNew RobiNew is offline Getting the last of three names/words Windows 10 Getting the last of three names/words Office 2016
Competent Performer
Getting the last of three names/words
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Thanks, Vivka! Now it's even shorter.
Reply With Quote
  #12  
Old 12-30-2023, 08:04 AM
RobiNew RobiNew is offline Getting the last of three names/words Windows 10 Getting the last of three names/words Office 2016
Competent Performer
Getting the last of three names/words
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Hi, Vivka! I'm again in trouble. I'm trying to modify the code below (taken from the one above, #9) so as to select all the words that precede the comma in the first paragraph. In other words I need to select from paragraph start to the comma. Can you help? Thanks!
Code:
Set oRng = ActiveDocument.Paragraphs(1).Range
oRng.Select
   If InStr(1, oRng, ",") > 0 Then
   oRng.Collapse
      oRng.MoveUntil Cset:=",", Extend
'Extend from paragraph start until comma
   End If
Reply With Quote
  #13  
Old 12-30-2023, 09:16 AM
vivka vivka is offline Getting the last of three names/words Windows 7 64bit Getting the last of three names/words Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Hi, RobiNew! Sometimes my lack of good knowledge helps me to find simple solutions. Here you are:
Code:
Sub LastNameRN()
'Select the range from the 1st para's start until the para's 1st comma.

Set oRng = ActiveDocument.Paragraphs(1).range
   If InStr(1, oRng, ",") > 0 Then 
      oRng.Collapse
       oRng.MoveEndUntil Cset:=","
       oRng.Select
   End If
End Sub
Reply With Quote
  #14  
Old 12-30-2023, 09:26 AM
RobiNew RobiNew is offline Getting the last of three names/words Windows 10 Getting the last of three names/words Office 2016
Competent Performer
Getting the last of three names/words
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Thank you, Vivka! Obviously, I was a bit confused while trying to write that code,
Reply With Quote
  #15  
Old 12-30-2023, 09:39 AM
vivka vivka is offline Getting the last of three names/words Windows 7 64bit Getting the last of three names/words Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

You are welcome, RobiNew!
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Foreign words/names Read Aloud HanselM Word 1 05-01-2023 09:36 AM
Getting the last of three names/words Removing microsoft build-in style names - display of style names in styles gallery changed Firebody Word 8 03-06-2022 08:22 AM
How to find (highlight) two and more words in a list of 75k single words in Word 2010 Usora Word 8 05-29-2018 03:34 AM
Need to extract domain names containing only specific words (MAJOR BULK) Maxwell314 Excel 4 12-08-2014 05:10 PM
How to enter names in Resource Pool/names pstein Project 1 03-26-2012 07:37 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:15 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