Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 04-20-2025, 11:06 PM
gcp gcp is offline Find and save bold capitalised words Windows 11 Find and save bold capitalised words Office 2021
Novice
Find and save bold capitalised words
 
Join Date: Mar 2025
Posts: 17
gcp is on a distinguished road
Default Find and save bold capitalised words


I am writing a macro to change the gender in a Will from the husband to the wife where they make complementary Wills. I have the following in a module. When I tried to insert the selected it was capitalised but not bold. Any clues?

Code:
Sub WillGenderChange21April()
'
' WillGenderChange21April Macro
Dim SavedTexts As Object
Dim rng As Range
Set rng = Selection.Range

Selection.HomeKey Unit:=wdStory  
    Selection.GoTo What:=wdGoToBookmark, Name:="Husband"
    SelectNameForWillSubSub  'This calls sub SelectNameForWillSubSub
    
    
    ' Move to a new location
    Selection.GoTo What:=wdGoToBookmark, Name:="Wife"
    If SavedTexts.Exists(Husband) Then
        Selection.FormattedText = SavedTexts(Husband).FormattedText 
   
    
    'SelectNameForWillSubSub  'This calls sub
    ' Insert the stored text at the new location
    Selection.TypeText Husband 'UserSelection

 
'It works to here to select the full name. Now to assign
'the selection to a variable and then delete it

End Sub

Sub SelectNameForWillSubSub()
'' SelectNameForWillSubSub Macro
'
Dim rng As Range
Set rng = Selection.Range
Dim selectedText As String
'SetSavedTexts = CreateObject("Scripting.Dictionary")
With rng.Find
        .ClearFormatting
        .Font.Bold = True
        .MatchCase = True
        .Text = "[A-Z]{1,}"
        .MatchWildcards = True
        .Execute
    End With

    ' If bold uppercase text is found, extend selection
    If rng.Find.Found Then
        Do While rng.Characters.Last.Font.Bold = True And _
                 rng.Characters.Last.Text Like "[A-Z ]" ' Allow spaces
            rng.MoveEnd wdCharacter, 1
        Loop
    End If

    ' Retract selection if a lowercase character was picked up
    If rng.Characters.Last.Text Like "[a-z]" Then
        rng.MoveEnd wdCharacter, -1
    End If
    rng.Select
  
End Sub

Last edited by macropod; 04-21-2025 at 12:16 AM. Reason: Added code tags for code formatting
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Find the Bold words Sainath Word 7 02-27-2024 10:30 PM
Find and save bold capitalised words Changing words with bold and italics and turning "regular" words into italicized and bold ones thiagoafdoria Word VBA 4 12-02-2019 11:41 AM
Find and save bold capitalised words Find number and words without a period and bold jeffreybrown Word 4 08-20-2019 05:58 PM
Find and save bold capitalised words VBA Find&Replace all bold, itlaic, underlined and highlighted words/characters Kalü Word VBA 22 04-24-2018 05:35 AM
Find and save bold capitalised words All words in document appear bold but are not! Ezra Word 4 07-31-2017 06:53 AM

Other Forums: Access Forums

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