Solved

Word macro to find and replace % to percent conditionally

Posted on 2012-04-12
4
485 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 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

PaperPort has a feature called the "Send To Bar". It provides a convenient, drag-and-drop interface for using other installed software, such as Microsoft Office. However, this article shows that the latest Office 2016 apps (installed with an Office …
My experience with Windows 10 over a one year period and suggestions for smooth operation
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

749 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