Numbering particular words in sentences contained in paragraphs

D

dado_maker

For every occurance of a particular word in a paragraph, I would like to
subscript a number right before the word.
I need to increment a interger var for each occurance replace the word
with the subscript and word back into the sentence, and after leaving
the paragraph return the var to 1 for the next paragraph(s).

Any help appreciated,
dado_maker
 
G

Greg Maxey

dado_maker,

This seems to work if I understand your objective. I am sure it is rough,
rough, rough and perhaps a master will be along to offer improvement.

Sub ScratchMacro()
Dim oPara As Paragraph
Dim oWord As Range
Dim i As Long
Dim myWord As String

myWord = InputBox("Enter the word to find.")

For Each oPara In ActiveDocument.Paragraphs
i = 0
For Each oWord In oPara.Range.Words
If Right(oWord, 1) = " " Then oWord.MoveEnd Unit:=wdCharacter,
Count:=-1
If LCase(oWord) = LCase(myWord) Then
i = i + 1
oWord.Collapse Direction:=wdCollapseStart
oWord.Select
'insert field, reset sequence to 1 for first occurence in paragraph
If i = 1 Then
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,
Text:= _
"SEQ WordNum \r1", PreserveFormatting:=True
Else
'insert field
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,
Text:= _
"SEQ WordNum", PreserveFormatting:=True
End If
'superscript the sequence number
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
With Selection.Font
.Superscript = True
End With
End If
Next
Next


End Sub
 

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