Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Process single characters from a Word document 1

Status
Not open for further replies.

VicRauch

Programmer
Sep 26, 2004
242
0
0
US
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:
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

Thanks,
Vic
 
Hi Vic,

There are various improvements that can be made to your code, the most important of which involve using ranges rather than selections. Here's some code for you to play with. It goes through the document looking for superscripted text and reports what it finds. It also finds any 'P' characters and superscripts them - after reporting what superscripted characters it had already found. So, if you run it twice on a document with nothing superscripted, the first time through it won't report anything, but on the second pass it'll report all 'P' characters.
Code:
Sub Inspector()
Dim pRange As Word.Range, oChr
For Each pRange In ActiveDocument.StoryRanges
  Do
    For Each oChr In pRange.Characters
    If oChr.Font.Superscript = True Then MsgBox oChr & vbCrLf & oChr.Font.Name
    If oChr = "P" Then oChr.Font.Superscript = True
    Next
    Set pRange = pRange.NextStoryRange
  Loop Until pRange Is Nothing
Next
End Sub
Cheers

[MS MVP - Word]
 
macropod,
Thank you for the code! I'll start playing with it and let you know later how it's going.
Vic
 
macropod,

Again, thanks for the code. But as usual, I need more.

Two things:
1. Where can I find some documentation with decent examples (read "not microsoft") as how to work with ranges and/or selections. And, .Find and Replace too. I know Access VBA very well, but when it comes to working with a string, I'm used to For x = 1 to ??? and then working with each character one at a time. I really like the idea of using the .Find and .Execute to do the work, but some GOOD reading material would be a big help.
2. I will be deleting anything in the text that is in superscript. Then, I will want to find any numbers that start a word, but there are other characters besides numbers that end the word. These numbers I will then assign to be in superscript.

Thanks!
Vic
 
Hi Vic,

There are lots of good examples of using ranges scattered throughout this forum. Take a look at Woody's too:

Word's macro recorder is also a useful tool to see how different things work, but it is limited to working with selections, the code is very verbose at times, and shows nothing of what can be done with loops and iterating through collections.

Having said that, I wonder whether you need a macro for what you've described. Word's Find/Replace function can delete all superscripted text and, if you use wildcards etc, you can find numbers that start a word, and so on. Check out the Help file.

Cheers

[MS MVP - Word]
 
macropod,
I have been looking around for over 4 or 5 hours now using Google to find what I can, but mostly no luck. Generally, when one knows what the actual words are, then help can be found, but when one only knows what is needed to be done, but not the correct words to use, that is when it becomes very frustrating. I have seen Woody's before, and I'm sure there are plenty of examples scattered throughout this forum. What I'm looking for is how to pick off the number from the front of the found word in a range and then make that number superscript. With no decent documentation to read, I still have NO idea how this stuff works. I know how it is done from the UI, and how to record a macro, but that comes nowhere close to being very helpful when doing something like picking the numbers off the front of a word and then setting them to be superscript.
I do want/need this to be in a macro, as I know I will be doing this task on a fairly regular basis, and do not want to be keying in a search mask each time.
I do believe I have all the looping down at this point; I just need either some excellent documentation on the Find/Replace and String handling ideas when dealing with ranges/selections. Truly, I do not have the patience to spend another day trying to find something that is scattered somewhere in this haystack.
If you could help with pointing to excellent documentation, or some actual coding examples, it would be greatly appreciated.
Vic
 
Hi Vic,

I really don't have much time to invest in this at the moment, but here's something to get you started:
Code:
Sub WordInspector()
Dim pRange As Word.Range, oChr, oWrd
For Each pRange In ActiveDocument.StoryRanges
  Do
    For Each oWrd In pRange.Words
      If IsNumeric(oWrd) = False And IsNumeric(Left(oWrd, 1)) = True Then
        With oWrd
          For Each oChr In oWrd.Characters
            If IsNumeric(oChr) = True Then
              oChr.Font.Superscript = True
            Else
              Exit For
            End If
          Next
        End With
      Else
        If oWrd.Characters(1).Font.Superscript = True Then oWrd.Delete
      End If
    Next
    Set pRange = pRange.NextStoryRange
  Loop Until pRange Is Nothing
Next
End Sub
The above code is quite similar to the first lot I posted, except that it first tests whole words and, if the whole word isn't a number but the first character is, then the leading numerics are superscripted; otherwise if the first character is superscripted, the word is deleted. I think this fits your statement:
I will be deleting anything in the text that is in superscript. Then, I will want to find any numbers that start a word, but there are other characters besides numbers that end the word. These numbers I will then assign to be in superscript.

You're unlikely to find any coding resource that is pre-coded for exactly what you want to do.

Cheers

[MS MVP - Word]
 
Hi Vic, the key concept on using .Find with a Range object is that each iteratation resizes the range.

Say you have a document with, oh, 20 paragraphs. The number does not matter.

Say you want to find a specific string. It could be anywhere. Say the string is "yadda".

Let's shorten it even more, two paragraphs. Again, the length does not matter.

Blah blah blah blah yadda blah. Yippee blah whatever yadda thing this and that.

Say you make a Range object (r) be the WHOLE document.
Code:
Dim r As Range
Set r = ActiveDocument.Range

So r (the Range object) IS the range covering:

Blah blah blah blah yadda blah. Yippee blah whatever yadda thing this and that.

OK? R covers the whole thing.

Code:
Dim r As Range
Set r = ActiveDocument.Range
With r.Find
    Do While .Execute(Findtext:="yadda", Forward:=True) _
            = True
        MsgBox r.Previous(Unit:=wdWord, Count:=1)
    Loop
End With

The very simple code above demonstrates the resizing of the range object when you use Find. It simply displays a messagebox of the previous word when Find finds "yadda".

Result?

Messagebox #1 = "blah". It is the previous word to the found "yadda".

Messagebox #2 = "whatever" It is the previous word to the NEXT found "yadda"

The range of any range object, when using Find, resizes TO that found range.

Start: r = the whole thing.

.Execute = True? r = "yadda" (the first one)
.Execute = True? r = "yadda" (the second one)

It is crucial to understand that IF you action the entire found (resized) range....
Code:
Dim r As Range
Set r = ActiveDocument.Range
With r.Find
    Do While .Execute(Findtext:="yadda", Forward:=True) _
            = True
        r.Font.Bold = True
    Loop
End With
which finds each "yadda" and makes it bold, then your Do loop can continue.

HOWEVER, if you resize the range, or do other actions that may change the resized range, then you most likely will have to do things like move the range forward, or collapse it. Otherwise you can get stuck in an infinite loop. Or....you can exit out of your Do loop prematurely.

faq219-2884

Gerry
My paintings and sculpture
 
Hi..

I found this thread while googling for a solution to a document formatting problem I have at the magazine I work at.

We get many word doc submissions with superscripted numbers, streets, etc (72nd St., 5th inning, and so forth).

We need to bring these down to a roman (baseline) character.

I'm just getting into writing my own code to do clean up of many problems, but can't seem to get a handle on this, as I can't get Word itself to do a search and replace on the format of these numbers.

Any help would be appreciated. TIA
 
NYMarsh,

This is what I have so far, but have not been able to work on it for a while. Hope to get back to it next week, but never know.
Code:
  For Each pRange In ActiveDocument.StoryRanges
'    Do
      With pRange.Find
          ' to ensure that unwanted formats aren't included as criteria
          .ClearFormatting
          .Replacement.ClearFormatting
          'You don't care what the text is
          .Text = ""
          'Find the Subscript text
          .Font.Subscript = True
          'Change to superscript
          .Replacement.Font.Superscript = True
          'delete all subscript text with superscript text
          .Execute Replace:=wdReplaceAll
'          .Font.Name
      End With
'    Loop Until pRange Is Nothing
  Next
Note in the code that there is a ".Font.Subscript = True" This is setting the pRange.Find to look for any text that is set to Subscript. Then the next line, .Replacement.Font.Superscript = True will change any found text to Superscript. Of course, this is what I was looking for.

For you, I would think that if you look for .Font.Superscript = True, then set whatever is found to: .Replacement.Font.Superscript = False that should take care of it. I've not tested this, so I don't know for sure.

Hope this helps.
Vic
 
Hi NYMarsh,

If you simply want to replace all suprescript in a document with the baseline font, you don't need a macro. Word's standard Find/Replace function will do the job quite well. Here's how:
. Open the Find/Replace window
. With the find box selected, click on More|Format|Font
. Click one on 'Superscript', then OK
. With the find box selected, click on More|Format|Font
. Click twice on 'Superscript', then OK
. Click 'Replace All'
If you're doing a lot of this, you could record a macro to automate it. It's pretty easy to do - and you even assign it to a toolbar button or a keyboard shortcut.

Cheers

[MS MVP - Word]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top