Consulting

Results 1 to 5 of 5

Thread: About adding autonumber with vba in MSWord

  1. #1

    About adding autonumber with vba in MSWord

    I have a variable called Rng1 (range) stored the followings:

    (a) The 1st line
    (b) The second line with some images as inlineshape
    and so on....

    i want to paste it onto another Range called Rng2.

    I use the following lines

    ActiveDocument.Rng2.FormattedText = Rng1

    It works, it give me the following

    (a) The 1st line
    (b) The second line with some images as inlineshape
    and so on....

    Then I want to add a auto numbering on the 1st line. I would like the result like the following
    1. (a) The 1st Line
    (b) The second line with some images as inlineshape
    and so on

    I tried the following line

    ActiveDocument.Rng2.paragraph(1).ListFormat.ApplyNumberDefault

    But the result give me

    1. The 1st Line
    (b) The second line with some images as inlineshape
    and so on

    It seems some auto correct function in Word eat my "(a)" and replace it with "1."

    Any ideas ?

    Many Thanks
    *first time to post in this forum, forgive me if i made any mistakes.

  2. #2
    If the letters (a) and (b) are plain text rather than autonumbers then the following should work. If they are autonumbers you will have to convert them to text first.

    Dim orng as range
    Set orng = orng2.Paragraphs(1).Range
        orng.Collapse 1
        orng.Fields.Add orng, wdFieldListNum
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    Quote Originally Posted by gmayor View Post
    If the letters (a) and (b) are plain text rather than autonumbers then the following should work. If they are autonumbers you will have to convert them to text first.

    Dim orng as range
    Set orng = orng2.Paragraphs(1).Range
        orng.Collapse 1
        orng.Fields.Add orng, wdFieldListNum
    Thanks for your answer.
    I tried. it works!
    But i want to make it perfect.

    a few requests
    i tried your code.

    Original
    (a) the 1st line
    (b) the 2nd line
    and so on...

    After
    1)(a) the 1st line
    (b) the 2nd line
    and so on...

    I want to change the 1) numbering to 1.[with a Tab] and control the indent.

    My imagine result
    1. (a) the 1st line<<<<<<someehow i cannot align the thing properly, hope you understand

    (b) the 2nd line
    and so on...

    May i have some direction?. Any tips?

    Many Thanks

  4. #4
    Assuming orng2 (not defined in your questions) is a selected range of paragraphs then
    Dim orng As Range, oRng2 As Range
    Dim i As Long
        
        With oRng2
            .ParagraphFormat.TabStops.ClearAll
            .ParagraphFormat.TabStops.Add Position:=InchesToPoints(0.25), _
                                          Alignment:=wdAlignTabLeft, _
                                          Leader:=wdTabLeaderSpaces
        End With
        oRng2.Paragraphs(1).Range.InsertBefore vbTab
        Set orng = oRng2.Paragraphs(1).Range
        orng.Collapse 1
        orng.Fields.Add orng, wdFieldListNum, "LegalDefault"
        For i = 2 To oRng2.Paragraphs.Count
            oRng2.Paragraphs(i).Range.InsertBefore vbTab
        Next i
    is one way of achieving your aim. Adjust the size of the tab setting to taste. Here .25"
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  5. #5
    Thx gmayor!

    your code seems using something called "field" to add a number in front of my 1st line of the paragraph. Your code work well with some of my situation ( i have a loop , need to repeat adding the number list ). But for some reasons (The formatting problem again with the tabs position, indent , etc). Your code cannot fits me for the whole documents (I need to repeat adding the number list for about 100+ times)

    i am guessing and finding the option in ms word that can disable the auto correct function which make my (a) auto corrected to 1. <<the number list
    Do you have any idea?
    If i found that option and disable it through the code. I think its the best solution i want.

    Many Thanks.

    1 more question.
    As i mentioned. i need to repeat add number list on some of the paragraph (Let say paragraph 1, paragraph 10, paragraph 12, paragraph 20,,,,,,,,,,,,,,,,)

    my stupid way is to set up a loop and goto each paragraph and add the number list which is very slow if i need to loop 500+ times

    is there a way to select all the paragraph at once (like store all the required paragraph in a range or something) then apply the format to that range??

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •