Macro to replace alphanumeric characters in selection with “x” and retain case

I have a macro that replaces all alphanumeric characters in a Word doc selection with an "x" (as a means of redacting) and it works just fine. However, I would like it to retain the case setting of each character it replaces with that "x". For example, if I select:

John A. Doe
56 Spruce Street
Apt 19
Anytown, NV 12345

I want the macro to produce this:

Xxxx X. Xxxx
xx Xxxxxx Xxxxxx
Xxx xx
Xxxxxxx, XX xxxxx

But I don't know how to adapt the code to accomplish this, or how to build a different approach to achieve these results. Can someone help? I've attached a test file with the macro.
Redaction-Test.doc
marrick13Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongCommented:
one way is after the first conversion, you do a "proper case" conversion.
0
marrick13Author Commented:
Yes, I could do that, and for simple examples such as the one I provided, that would work. But suppose I wanted to redact a whole or partial paragraph? That would have 'sentence' case but could also include some words that were all caps, proper, lower case, or a mix. What I was looking for was a means of replacing alphanumeric characters in a selection with "x" or "X", depending on the original case, rather than manually adjusting the case (which would be difficult for larger pieces of text since it would be all 'x's and I would have to manually compare the original with the redacted version).
0
GrahamSkanRetiredCommented:
See if this helps
'...
If findHL(uSelection) Then
MsgBox "Letters: " & CountLetter & vbCr _
    & "Numbers: " & CountNum & vbCr _
    & "Non-alphanumeric: " & TotalChar - (CountLetter + CountNum + CountSpace) & vbCr _
    & "Spaces: " & CountSpace & vbCr _
    & "Total: " & TotalChar & vbCr & vbCr _
    & "Characters converted to 'x' in selection: " & CountNum + CountLetter, vbOKOnly, "Replacement Result"
  
End If
End Sub

Function findHL(r As Range) As Boolean
    Dim bFoundLower As Boolean
    Dim bFoundUpper As Boolean
    
    With r.Find
        .Text = "[a-z0-9]{1}"
        .Replacement.Text = "x"
        .MatchWildcards = True
        bFoundLower = .Execute(Replace:=wdReplaceAll)
    End With
    
    With r.Find
        .Text = "[A-Z]{1}"
        .Replacement.Text = "X"
        .MatchWildcards = True
        bFoundUpper = .Execute(Replace:=wdReplaceAll)
    End With
    findHL = bFoundLower Or bFoundUpper
End Function

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

marrick13Author Commented:
That is perfect! Thanks so much, Graham!
0
Ryan ChongCommented:
raise the wrong request? I have rejected the Delete request.
0
marrick13Author Commented:
Macro does the right job.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.