#1
|
|||
|
|||
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 |
#2
|
|||
|
|||
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 |
#3
|
|||
|
|||
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 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 |
#4
|
|||
|
|||
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.
|
#5
|
|||
|
|||
Thank you, Robinew, for your kind words! I have good teachers here and I'm still learning. Season's greetings!
|
#6
|
|||
|
|||
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 |
#7
|
|||
|
|||
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 |
#8
|
||||
|
||||
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 |
#9
|
|||
|
|||
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 |
#10
|
|||
|
|||
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.
|
#11
|
|||
|
|||
Thanks, Vivka! Now it's even shorter.
|
#12
|
|||
|
|||
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 |
#13
|
|||
|
|||
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 |
#14
|
|||
|
|||
Thank you, Vivka! Obviously, I was a bit confused while trying to write that code,
|
#15
|
|||
|
|||
You are welcome, RobiNew!
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Foreign words/names Read Aloud | HanselM | Word | 1 | 05-01-2023 09:36 AM |
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 |