Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-07-2017, 11:33 AM
KeviM KeviM is offline Attempting to find text, select words before it, and format the entire selection Windows 7 64bit Attempting to find text, select words before it, and format the entire selection Office 2013
Novice
Attempting to find text, select words before it, and format the entire selection
 
Join Date: Apr 2017
Posts: 1
KeviM is on a distinguished road
Default Attempting to find text, select words before it, and format the entire selection

Hello all,

I've been trying unsuccessfully to search for a phrase, select 2 words before it, then italicize the text (including the phrase I searched for). The code I have below is entirely unsuccessful, and somehow just deletes "and Michael". Any suggestions?



Sub Macro2()

Selection.Find.ClearFormatting
With Selection.Find
.Text = "and Michael"
.Replacement.Text = "and Michael"
.Forward = True
.Wrap = wdFindStop
Do While .Execute() = True
Selection.TypeParagraph
Selection.MoveLeft Unit:=wdWord, Count:=2, Extend:=wdExtend
Selection.Find.Replacement.Font.Italic = True
Selection.Font.Bold = True
Selection.Collapse Direction:=wdCollapseEnd
Loop
End With
End Sub
Reply With Quote
  #2  
Old 04-07-2017, 05:36 PM
macropod's Avatar
macropod macropod is offline Attempting to find text, select words before it, and format the entire selection Windows 7 64bit Attempting to find text, select words before it, and format the entire selection Office 2010 32bit
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

You probably don't even need a macro for this - you should be able to do it with a wildcard Find/Replace, where:
Find = <[! ]@ [! ]@ and Michael
Replace = ^&
and you set the replacement format to bold.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Attempting to find text, select words before it, and format the entire selection Headers - Find text, Select, Pass value to Varable BrotherDude Word VBA 4 07-15-2015 05:42 PM
Microsoft Word macro to find text, select all text between brackets, and delete helal1990 Word VBA 4 02-05-2015 03:52 PM
Attempting to find text, select words before it, and format the entire selection Help required with Selection.Find to search a text file. Marko Word VBA 2 10-18-2014 06:32 AM
VBA to Find and Format Text string defined using Inputbox within selection sistemalan Word VBA 7 10-03-2014 07:28 AM
Attempting to find text, select words before it, and format the entire selection How to find and select text in a document? mkhuebner Word VBA 8 02-04-2014 08:04 PM

Other Forums: Access Forums

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