Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
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: 200
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
  #2  
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
Expert
 
Join Date: Jul 2023
Posts: 293
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
Reply

Thread Tools
Display Modes


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 05:55 AM.


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