?
Solved

Word macro to find and replace % to percent conditionally

Posted on 2012-04-12
4
Medium Priority
?
489 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Suggested Courses

770 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