Consulting

Results 1 to 4 of 4

Thread: Help VBA automatic numbering in Word

  1. #1
    VBAX Regular
    Joined
    Mar 2022
    Posts
    8
    Location

    Help VBA automatic numbering in Word

    I have a document in Word of the form:
    Question 1: Chdsh
    Question 1: aghj
    Lesson 1: ghj
    Lesson 2: sagdhj

    Is there a way to quickly change the "Question 1, .." and "Lesson 1, ..." to automatic numbering? Thanks everyone

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    If you created a new list style called say QL List and made the Level 1 number format "Question 1" and the Level 2 number format "Lesson 1" then you could use a find and replace something like this:

    Sub A()
    Dim oRng As Range
      Set oRng = ActiveDocument.Range
      With oRng.Find
        .Text = "Question [0-9]@: @"
        .MatchWildcards = True
        While .Execute
          oRng.Delete
          oRng.Paragraphs(1).Range.Style = "QL List"
          oRng.Collapse wdCollapseEnd
        Wend
      End With
      Set oRng = ActiveDocument.Range
      With oRng.Find
        .Text = "Lesson [0-9]@: @"
        .MatchWildcards = True
        While .Execute
          oRng.Delete
          oRng.Paragraphs(1).Range.Style = "QL List"
          oRng.ListFormat.ListLevelNumber = 2
          oRng.Collapse wdCollapseEnd
        Wend
      End With
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Regular
    Joined
    Mar 2022
    Posts
    8
    Location
    Oh sorry it doesn't work

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    If is doesn't work then apparently you didn't properly create and define a new list style titled "QL List" or applied the code wrong. "Sorry, it doesn't work" isn't going to get you much help here.
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

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