Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find & Replace string

Status
Not open for further replies.

radder33

IS-IT--Management
Nov 26, 2009
66
DE
Hello

I am trying to run a find and replace on text strings in a word document. The strings are always prefixed as they are products. IE Product@-TextString.

What I want to do is create a find and replace so that when it finds Product@-* it makes all the different strings following Product@- bold.

I am hoping to do this with one find and replace rather than create a macro to perform for every task. Any help would be great.
 
You will need a macro, but it may be possible to have the same macro process all of the data. However, you haven't provided a crucial piece of information: what defines the textstring to be bolded. For example:
• is it alpha-numeric, numeric, alpha?
• an it include spaces and if so, how many minimum and maximum?
• does it already have any particular formatting?

Cheers
Paul Edstein
[MS MVP - Word]
 
All of the strings are alpha and have no spaces but they can be varying lengths.

All start Product@- so for example we could have

Product@-axe-long
Product@-Spanner
Product@-tool

I was hoping the prefix could pin the replacement but unsure on the whether the variance in string length would make this possible?
 
Try:
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim Rng As Range
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Text = "Product\@-[A-Za-z\-]{1,}"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = True
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute
  End With
  Do While .Find.Found
    Set Rng = .Duplicate
    With Rng
      .Start = .Words(3).Start
      .Font.Bold = True
    End With
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
Application.ScreenUpdating = True
End Sub

Cheers
Paul Edstein
[MS MVP - Word]
 
You can't do it with a single Replace, but you could do it with two simple ones.

First, using the Find string in Paul's code (with Use Wildcards checked), click in the Replace With box and press Ctrl+B, then click Replace All. This will Bold the complete string. Then delete all the guff a the end of the Find string (to leave "Product\@-"), go back to the Replace With box and press Ctrl+B again, then click Replace All again; this will un-Bold the prefix.


Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top