Solved

Word macro to find and replace % to percent conditionally

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
My experience with Windows 10 over a one year period and suggestions for smooth operation
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
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 …

896 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

18 Experts available now in Live!

Get 1:1 Help Now