Solved

Word macro to find and replace % to percent conditionally

Posted on 2012-04-12
4
481 Views
Last Modified: 2012-04-13
Hello, I would like to get some assistance in writing a find and replace Word 2007 macro from the folks here.  I would like the macro to perform two conditional operations.

First, for any text that is not enclosed by ( ) or [ ] find the % sign and change it to the word 'percent' without the single quotes.  

Next, find any instances of percent enclosed by ( ) or [ ] and change this to the % symbol.  

Any help would be greatly appreciated as always.  

Thanks,
Bev
0
Comment
Question by:Bevos
  • 2
  • 2
4 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 37839403
Can you clarify, please?

"for any text that is not enclosed by ( ) or [ ] ". 'Any text' is a bit vague. It could be a whole document, a paragraph, a word, one character or anything in between.

For that which is already enclosed, what needs to be changed to a % symbol?

Perhaps a few examples would help.
0
 

Author Comment

by:Bevos
ID: 37839480
Hi Graham, thanks so much for the response here is an example:

When I said any text I mean that the whole word document would be scanned for these changes.  Sorry if there was confusion by the vague wording.  

Before correction:
This is an example paragraph in which % is used incorrectly.  However if someone uses parentheses then (percent) should use a symbol instead.  This should be the case even if large amounts of text are in parentheses.  [This example of percent should be informative %].  So basically percent is spelled out whenever not enclosed by parentheses or brackets.


After correction:
This is an example paragraph in which percent is used incorrectly.  However if someone uses parentheses then (%) should use a symbol instead.  This should be the case even if large amounts of text are in parentheses.  [This example of % should be informative %].  So basically percent is spelled out whenever not enclosed by parentheses or brackets.
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 37840403
Sorry for the delay. It was quite difficult to find text that is not in brackets. Hopefully you aren't already using highlighting because this code uses temporary highlighting to differentiate.
Sub Percent()
    Dim rng1 As Range
    
    Set rng1 = ActiveDocument.Range
    Application.Options.DefaultHighlightColorIndex = wdYellow
    With rng1.Find
        .MatchWildcards = True
        .Text = "\(*\)"
        .Replacement.Highlight = True
        .Execute Replace:=wdReplaceAll
        
        .Text = "\[*\]"
        .Replacement.Highlight = True
        .Execute Replace:=wdReplaceAll
        
        .MatchWildcards = False
        .Text = "percent"
        .Highlight = True
        .Replacement.Text = "%"
        .Execute Replace:=wdReplaceAll
        
        .Text = "%"
        .Highlight = False
        .Replacement.Text = "percent"
        .Execute Replace:=wdReplaceAll
        
    End With
    ActiveDocument.Range.HighlightColorIndex = wdNoHighlight
End Sub

Open in new window

0
 

Author Closing Comment

by:Bevos
ID: 37842949
Perfect as always thank you Graham!
0

Featured Post

A Knowledge Base That Stays Up-to-Date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now