how to sequentially add a number to each word in MS word file ?

I am trying to individualize each word in MS word according to its order. 

and I thought by numbering it would accomplish my goal. 

for example 

"1Create 2a 3new 4question 5or 6start 7a 8discussion"

If I can number all the words like so, I will be able to distinguish 2a , 7a,

any ideas on how I can do this? 


I am trying to apply different text setting to each word according to its position in the sentence. 

thanks! 


While your idea of what constitutes a 'word' may not agree with the program's view, the following macro will do what you ask. Select the text to be marked and run the macro.

http://www.gmayor.com/installing_macro.htm   

Sub Macro1()
Dim oWord As Object
Dim i As Long
    i = 1
    For Each oWord In Selection.Range.Words
        oWord.InsertBefore i
        i = i + 1
    Next oWord
End Sub

Graham Mayor (Microsoft Word MVP 2002-2019)
For more Word tips and downloads visit my web site
https://www.gmayor.com/Word_pages.htm

1 person found this reply helpful

·

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Hi thank you for the quick reply 

it does a fine job, it is just as what you said. 

It applies the numbers to all the objects including, punctuation mark,  which I should like to assign with a single number 

E.G 

I wanted to assign the word "didn't" with a single number

but it assigns 3 numbers instead - > 1didn2'3t

with your effort I think I now know what would really accomplish what I had in mind

can you change the code so that it fills the empty blank space with the sequential number?

since all the "word"s are separated by "space" , assigning a space with the sequential numbers will do the same job but with more resemblance toward the view of the "word" as a normal person would have. 

thanks! 

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

You don't really want to "fill" the blank space with the number, as that would make each entire paragraph into one word, and it would no longer show the automatic line breaks properly. Instead, you need to add a number after each space. Also, in order to number the first word of each paragraph you need to add a number at the start of the document and after each paragraph mark. This macro will take care of that.

Sub NumberWords()
    Dim rg As Range
       
    ActiveWindow.View.ShowFieldCodes = False
       
    ' place a SEQ field at the start of the document
    Set rg = ActiveDocument.Range
    rg.Collapse wdCollapseStart
    ActiveDocument.Fields.Add Range:=rg, Type:=wdFieldSequence, _
        Text:="wd", PreserveFormatting:=False
    
    ' find each "white space" (incl. multiple spaces, tabs, nonbreak spaces)

    ' and insert the field after it
    Set rg = ActiveDocument.Range
    With rg.Find
        .Text = "^w"
        .Wrap = wdFindStop
        While .Execute
            rg.Collapse wdCollapseEnd
            ActiveDocument.Fields.Add Range:=rg, Type:=wdFieldSequence, _
                Text:="wd", PreserveFormatting:=False
            rg.Collapse wdCollapseEnd
        Wend
    End With
    
    ' find each paragraph mark and insert the field after it
    ' unless it's the last one in the document
    Set rg = ActiveDocument.Range
    With rg.Find
        .Text = "^p"
        .Wrap = wdFindStop
        Do
            .Execute
            rg.Collapse wdCollapseEnd
            If rg.End = ActiveDocument.Range.End - 1 Then Exit Do
            ActiveDocument.Fields.Add Range:=rg, Type:=wdFieldSequence, _
                Text:="wd", PreserveFormatting:=False
            rg.Collapse wdCollapseEnd
        Loop
    End With
    
    ' update all fields
    ActiveDocument.Fields.Update
End Sub

_____________________________
https://jay-freedman.info

3 people found this reply helpful

·

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated April 9, 2024 Views 1,651 Applies to: