Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-02-2022, 10:30 AM
karkey karkey is offline find and replace with format Windows 10 find and replace with format Office 2013
Novice
find and replace with format
 
Join Date: Jan 2021
Posts: 19
karkey is on a distinguished road
Default find and replace with format

While replace I need to italic and highlight the word (et^sal).

Code:
Sub etalnew()
  Dim rngStory As Range
  For Each rngStory In ActiveDocument.StoryRanges
    
    Options.DefaultHighlightColorIndex = wdDarkYellow
    Selection.Find.Replacement.Highlight = True
    
    With rngStory.Find
      .Text = "<et al>"
      .Replacement.Text = "et^sal"
      .Replacement.Highlight = True
      .Wrap = wdFindContinue
      .MatchWildcards = True
      .Execute Replace:=wdReplaceAll
    End With
  Next rngStory
End Sub


Last edited by macropod; 08-02-2022 at 02:54 PM. Reason: Added code tags to restore formatting
Reply With Quote
  #2  
Old 08-02-2022, 02:14 PM
Souriane Souriane is offline find and replace with format Windows 10 find and replace with format Office 2019
Advanced Beginner
 
Join Date: Feb 2017
Location: Quebec, Canada
Posts: 82
Souriane is on a distinguished road
Default

Your code work just fine. There is only this line missing :

Code:
Selection.Find.Replacement.Font.Italic = True
under
Code:
Selection.Find.Replacement.Highlight = True
Reply With Quote
  #3  
Old 08-02-2022, 03:04 PM
macropod's Avatar
macropod macropod is offline find and replace with format Windows 10 find and replace with format Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try:
Code:
Sub etalnew()
Application.ScreenUpdating = False
Options.DefaultHighlightColorIndex = wdDarkYellow
Dim rngStory As Range
For Each rngStory In ActiveDocument.StoryRanges   
  With rngStory.Find
    .Text = "<et al>"
    .Replacement.Text = "et^sal"
    .Replacement.Highlight = True
    .Replacement.Font.Italic = True
    .Wrap = wdFindContinue
    .MatchWildcards = True
    .Execute Replace:=wdReplaceAll
  End With
Next rngStory
Application.ScreenUpdating = True
End Sub
Do note that textboxes, for example, won't be processed by such code and that Find/Replace is a bit flakey with page headers & footers. If you have 'et al' in any of those, you might need different code. See: Using a Macro to Replace Text Wherever It Appears in a Document
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
find and replace with format find/replace date format improvement? LQuinn Word VBA 2 05-14-2021 11:15 PM
Find/Replace using format of cell catflap Excel 1 09-11-2017 07:28 AM
find and replace with format Find and Replace maintain format winningson Word 3 01-19-2013 05:38 AM
find and replace with format Find and Replace Format macro issue Jack Word VBA 2 12-12-2012 09:24 PM
find and replace with format Bad view when using Find and Find & Replace - Word places found string on top line paulkaye Word 4 12-06-2011 11:05 PM

Other Forums: Access Forums

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