Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Word macro to find and replace % to percent conditionally

Posted on 2012-04-12
4
Medium Priority
?
490 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article describes how to import an Outlook PST file to Office 365 using a third party product to avoid Microsoft's Azure command line tool, saving you time.
Outlook for dependable use in a very small business   This article is about using the Outlook application (part of Microsoft Office) in a very small business, or for homeowners where dependability and reliability are critical requirements. This …
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.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Suggested Courses

604 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