How to process individual characters in a Word document?

G

GolferGuy

I have a need to examine each character in a Word document, and then,
depending on the context, remove a group of characters, change the formatting
of the character or group of characters. But I'm not sure what the objects
are to work my way through all the characters in a document. The code below
is what I have so far, in a testing mode to see what I can learn. One thing
I have learned, is that the first character of a group of characters that are
selected as "superscript" shows as not being superscripted. And one character
past the group shows as being a "superscript" character. I'm thinking I doing
something wrong. Here's my code:

Sub InspectEachCharacter()
Dim x As Integer
Dim MyText As String
ActiveDocument.Select
MyText = Selection
Debug.Print Len(MyText)
ActiveDocument.Characters(1).Select
With Selection
Debug.Print .Text, .Font.Name, .Font.Superscript
End With
For x = 1 To Len(MyText)
Selection.MoveRight unit:=wdCharacter, Count:=1
With Selection
Debug.Print .Text, .Font.Name, .Font.Superscript
'The following three lines of code will cause the whole word that starts
' with "P" to be set to "Superscript"
' But I don't know why????
If .Text = "P" Then
.Font.Superscript = True
End If
End With
Next x
End Sub
 
D

Doug Robbins - Word MVP

I am not really sure what you are doing (or why), but

Dim i As Long
With ActiveDocument
For i = 1 To .Characters.Count
If .Characters(i) = "P" Then
.Characters(i).Font.Superscript = True
End If
Next i
End With

will only superscript the letter P, not the whole word in which it appears.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

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