How do you remove control characters from a document

C

Charlie Mac

I have a routine that scans a document for character patterns to
locate specific words. Some documents contain hidden control
characters that will cancel the scan. I am using this code to rid
these characters, but I do not know what additional character codes to
remove. I cannot use the clearformatting command for the whole
document as the document's formatting (e.g. bold and italics) would be
history. Please help.

Fray(1, 1) = "^13"
Fray(1, 2) = "^p"
Dim MyRange As Range
Set MyRange = Selection.Range
MyRange.WholeStory
For Eye = 1 To 10
With Selection.Find
.Text = Fray(Eye, 1)
.Replacement.Text = Fray(Eye, 2)
.Forward = True
.Wrap = wdFindContinue
.Format = False
.Execute Replace:=wdReplaceAll
End With
Next Eye


Thank you.

Charlie from Texas
 
J

Jezebel

Control characters in a Word document have nothing to do with bold and
italics.

Control characters are defined as those with ASCII values less than 32. Your
search code might also get confused if there are unicode characters in
there. You can find all such with code like this --

Dim pChar As Word.Range

For Each pChar In ActiveDocument.Content.Characters

If AscW(pChar) < 32 Or AscW(pChar) > 255 Then
Debug.Print AscW(pChar)
End If
Next


By the way, ^p and ^13 are the same thing, so replacing one with the other
will do nothing at all.
 
C

Charlie Mac

Jezebel,

OK, I ran this code and it returns several 8216 and 8217 values plus
a few 13's. The H"s were just to separate returned values. What are
the 8216 and 8217 and how do I get rid of them? My code for replacing
them does not remove them.

Dim pChar As Word.Range, hold As String
For Each pChar In ActiveDocument.Content.Characters
If AscW(pChar) > 255 Then
hold = hold & AscW(pChar) & "HHHH"
End If
Next
MsgBox hold


Thank you for your reply.

Charlie from Texas
 
T

Tony Jollans

8216 and 8217 are curly left and right single quotes; I doubt you want to
get rid of them.
 
C

Charlie Mac

Tony,

Thank you. So...where is this info listed...how did you know?

Thanks,

Charlie form Texas
 
T

Tony Jollans

Hi Charlie,

These two are common and I just knew them (I can't remember how I first
found out).

In general if I want to know what a code is (or more likely the code for a
particular character) I google for it. With all searches a bit of trial and
error may be required but I have just tried 'unicode 8216' as a search term
and it quickly provided the answer for this one.
 
J

Jezebel

You can enter the character into a document and see what you get, or look it
up in the Insert > Symbol dialog. Bear in mind that
ASCW() returns a decimal value, so you need to convert to hex first.

To enter the character into a document, type the hex value (2018 or 2019 in
this case) and press Alt-X.
 
C

Charlie Mac

Tony,

Thank you.

I made a routine to find and replace "^14" through "^31" and "^127"
through "^255" They are replaced with "" to no avail. After it runs,
some non printable character still causes my text scanner (looking for
specific words) to skip a section of the document. The clear
formatting command will solve it but the bold and italics formatting
are also history. Is their any way to see the code used in clear
formatting...other than .clearformatting? A reveal codes button would
be nice.

This is my text search statement...I don't see any problems in it....

Selection.Find.Execute(FindText:= "test word", Forward:=True,
Format:=True, Wrap:=wdFindStop) = True

I appreciate your thoughts.

Take care,

Charlie from Texas
 
J

Jezebel

The problem with your code is the 'Format:=TRUE' argument -- that is saying
that the find should find a match only where it is formatted as specified in
the Find object's format settings. Use ClearFormatting and Format:=FALSE if
format is irrelevant to your search.

There is no 'reveal codes' option in Word, because Word doesn't work that
way. Formatting is not applied via codes embedded in the text.

Another possible problem is if the text you are looking for is not in the
body of the document -- eg in textboxes, footnotes, etc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top