Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-07-2015, 06:39 PM
paik1002 paik1002 is offline Find, select, and replace part of text with bold Windows 7 64bit Find, select, and replace part of text with bold Office 2010 64bit
Advanced Beginner
Find, select, and replace part of text with bold
 
Join Date: Dec 2015
Posts: 63
paik1002 is on a distinguished road
Default Find, select, and replace part of text with bold

I'd like to find a text in a selection and change only a part of the found text by boldfacing it.

For example, first find the text "hello there" in the present selection (e.g. first page of document), then boldface only "there".



Could it be done?


I am struggling with the following sort of VB code. FYI, wildcards are in use.

Selection.Find.ClearFormatting
Selection.Find.Replacement.Font.Bold = True (??????????)
With Selection.Find
.text = "(hello)*(there)"
.Replacement.text = (???????????)
.Forward = True
.MatchWildcards = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Reply With Quote
  #2  
Old 12-07-2015, 06:51 PM
macropod's Avatar
macropod macropod is offline Find, select, and replace part of text with bold Windows 7 64bit Find, select, and replace part of text with bold Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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 could use a macro like:
Code:
Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = InputBox("What is the Text to Find")
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute
  End With
  Do While .Find.Found
    .Words.First.Font.Bold = False
    .Words.Last.Font.Bold = True
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
Application.ScreenUpdating = True
End Sub
PS: When posting code, please use the code tags, indicated by the # button on the posting menu
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 12-07-2015, 07:15 PM
paik1002 paik1002 is offline Find, select, and replace part of text with bold Windows 7 64bit Find, select, and replace part of text with bold Office 2010 64bit
Advanced Beginner
Find, select, and replace part of text with bold
 
Join Date: Dec 2015
Posts: 63
paik1002 is on a distinguished road
Default

Thank you, but I can't get it to work.

Code:
Sub Demo()
    Application.ScreenUpdating = False
 
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .text = "(hello)*(there)"
        .Replacement.text = ""
        .Forward = True
      .MatchWildcards = True
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute
      End With
 
      Do While .Find.found
        Words.First.Font.Bold = False
        Words.Last.Font.Bold = True
        .Collapse wdCollapseEnd
        .Find.Execute
      Loop
    End With
    Application.ScreenUpdating = True
End Sub
I modified my code by incorporating your code into it, but I get a 424 run-time error,

Code:
        Words.First.Font.Bold = False
Reply With Quote
  #4  
Old 12-07-2015, 07:47 PM
macropod's Avatar
macropod macropod is offline Find, select, and replace part of text with bold Windows 7 64bit Find, select, and replace part of text with bold Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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're missing the period in front of 'Words' (i.e. '.Words').
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 12-07-2015, 11:24 PM
paik1002 paik1002 is offline Find, select, and replace part of text with bold Windows 7 64bit Find, select, and replace part of text with bold Office 2010 64bit
Advanced Beginner
Find, select, and replace part of text with bold
 
Join Date: Dec 2015
Posts: 63
paik1002 is on a distinguished road
Default

I overlooked on that. Thank you. It is working now.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find, select, and replace part of text with bold Select text to be underline and bold ksigcajun Word VBA 1 03-16-2015 07:51 PM
Microsoft Word macro to find text, select all text between brackets, and delete helal1990 Word VBA 4 02-05-2015 03:52 PM
Find, select, and replace part of text with bold Find/Replace Wildcard Needed-Bold & Highlight rsrasc Word VBA 3 11-11-2014 03:55 PM
Find, select, and replace part of text with bold find and replace in bold redzan Word VBA 1 07-27-2014 03:35 PM
how to search and replace BOLD text >> font color change? dylansmith Word 4 03-12-2013 09:51 PM

Other Forums: Access Forums

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