Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-04-2023, 10:19 AM
MLe MLe is offline Find out how to change all letter of first word using VBA ? Windows 11 Find out how to change all letter of first word using VBA ? Office 2021
Novice
Find out how to change all letter of first word using VBA ?
 
Join Date: Aug 2023
Posts: 1
MLe is on a distinguished road
Default Find out how to change all letter of first word using VBA ?


Hi,
Sorry Sorry sorry for my english

For my dissertation today at more than 400 pages, I would like to change the style and box of certain words. I found my happiness ... well almost.
In the example below, I need to modify "DESCRIPTION" in one style and "-- remplissage" in another style
This is my macro :

Code:
Sub change ()
Application.ScreenUpdating = False
Selection.HomeKey Unit:=wdStory
    Do
        With Selection.Find
            .ClearFormatting
            .Text = "COORDINATION"
            .MatchWholeWord = True 
            .Font.Bold = False
            .Font.Underline = True 
            .Style = "Souligné"
            .MatchCase = True
            .Wrap = wdFindStop
            .Execute
        End With
    If Selection.Find.Found Then
    Selection.Style = ActiveDocument.Styles("NORMAL_01")
               Selection.Range.Case = wdUpperCase
                 End If
     Loop Until Not Selection.Find.Found
End Sub
It's good except that it changes ALL the words "description" and I only want if this one is in capitals or if this one is at the beginning of a sentence

EX :
DESCRIPTION (yes)
This is a description (NO) de my car ...

Thanks in advance and sorry for my english
Reply With Quote
  #2  
Old 08-05-2023, 02:18 AM
gmayor's Avatar
gmayor gmayor is offline Find out how to change all letter of first word using VBA ? Windows 10 Find out how to change all letter of first word using VBA ? 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

The following will change the style of the word 'description' if in upper case or starting a sentence.
Note that the macro calls a character style - here the built-in style 'Strong'. If you call a paragraph style the chances are that the whole paragraph containing the word will be so formatted, so I suggest creating a character style to achieve the formatting you require.
Code:
Sub Macro1()
Dim oRng As Range
Dim vFind As Variant
Dim i As Integer
    vFind = Array("DESCRIPTION", "Description")
    For i = 0 To UBound(vFind)
        Set oRng = ActiveDocument.Range
        With oRng.Find
            .ClearFormatting
            .Text = vFind(i)
            Do While .Execute(findText:=vFind(i), MatchWholeWord:=True, MatchCase:=True)
                oRng.Style = "Strong" 'Change as appropriate
            Loop
        End With
    Next i
    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
Reply

Tags
allcaps matchcase



Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a way to only find the first letter of each line? (Word 97) Genericname1111 Word 4 09-11-2019 06:26 PM
Find a letter or symbol with a specific font Lou_Reed Word 1 11-15-2018 09:14 AM
Change Letter Format in Word GR_mahdi Word VBA 1 06-12-2017 08:24 PM
Find out how to change all letter of first word using VBA ? Find and replace with a macro, problem with letter case garcanrya Word VBA 2 01-10-2014 05:40 AM
if letter change font also change gsrikanth Excel 1 05-15-2012 03:03 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:16 PM.


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