none
Word 2007 VBA: Format a Numbered List - Problems RRS feed

  • Question

  • I am automating Word from Excel and want to start a new document and fill it in.  It is all working famously, except...  I create a document here and begin my list formating here:

      Dim LT As Word.ListTemplate
      
      Set objWord = New Word.Application
      objWord.Visible = True
      Set wdDoc = objWord.Documents.Add()
      Set LT = wdDoc.ListTemplates.Add(OutlineNumbered:=True)
        
    'Format Outline List for Specification
    'Format taken from existing 'standard' screen spec(s)
    
      SetupStyles
      
      With LT.ListLevels(1)
        .NumberFormat = "PART %1 -"
        .TrailingCharacter = Word.WdTrailingCharacter.wdTrailingTab
        .NumberStyle = Word.WdListNumberStyle.wdListNumberStyleArabic
        .NumberPosition = InchesToPoints(0)
        .TextPosition = InchesToPoints(0.25)
        .TabPosition = 9999999
        .ResetOnHigher = 0
        .StartAt = 1
        With .Font
          .Bold = True
          .Size = 14
        End With
        .LinkedStyle = ""
      End With
      With LT.ListLevels(2)
        .NumberFormat = "%1.%2."
        .TrailingCharacter = Word.WdTrailingCharacter.wdTrailingTab
        .NumberStyle = Word.WdListNumberStyle.wdListNumberStyleArabic 'wdListNumberStyleUppercaseLetter
        .NumberPosition = InchesToPoints(0)
        .TextPosition = InchesToPoints(0.375)
        .TabPosition = wdUndefined
        .ResetOnHigher = 1
        .StartAt = 1
        With .Font
          .Bold = False
          .Size = 12
        End With
        .LinkedStyle = ""
      End With

    I then extract values from my excel spreadsheet and fill out my document with the following code:

      wdDoc.ActiveWindow.Selection.Range.ListFormat.ApplyListTemplateWithLevel ListTemplate:= _
        LT, ContinuePreviousList:=False, ApplyTo:=wdListApplyToWholeList, _
        DefaultListBehavior:=wdWord10ListBehavior
    
      rowNum = 2
      paraLvl = Worksheets("Specification").Cells(rowNum, 1).Value
      paraTxt = Worksheets("Specification").Cells(rowNum, 2).Value
      While paraLvl <> 0
        If paraTxt <> "" Then
          With wdDoc.ActiveWindow.Selection
            .Range.SetListLevel Level:=paraLvl
            Select Case paraLvl
            Case 1
              .Range.Style = myStyle1
              .ParagraphFormat.SpaceBefore = 24
              .ParagraphFormat.SpaceAfter = 24
              .ParagraphFormat.SpaceAfterAuto = True
              .ParagraphFormat.SpaceBeforeAuto = True
              .Font.Size = 14
              .Font.Bold = True
            Case 2
              .Range.Style = myStyle2
              .ParagraphFormat.SpaceBefore = 12
              .ParagraphFormat.SpaceAfter = 12
              .ParagraphFormat.SpaceAfterAuto = True
              .ParagraphFormat.SpaceBeforeAuto = True
              .Font.Size = 12
              .Font.Bold = False
            Case 3
              .Range.Style = myStyle3
              .ParagraphFormat.SpaceBefore = 3
              .ParagraphFormat.SpaceAfter = 3
              .ParagraphFormat.SpaceAfterAuto = True
              .ParagraphFormat.SpaceBeforeAuto = True
              .Font.Size = 12
              .Font.Bold = False
            Case Else
              .Range.Style = myStyle3
              .ParagraphFormat.SpaceBefore = 3
              .ParagraphFormat.SpaceAfter = 3
              .ParagraphFormat.SpaceAfterAuto = True
              .ParagraphFormat.SpaceBeforeAuto = True
              .Font.Size = 12
              .Font.Bold = False
            End Select
            .TypeText Text:=paraTxt
            .TypeParagraph
          End With
        End If
        rowNum = rowNum + 1
        paraLvl = Worksheets("Specification").Cells(rowNum, 1).Value
        paraTxt = Worksheets("Specification").Cells(rowNum, 2).Value
      Wend
    

    Everything is working beautifully except that the spacing before and after is all the same.  I am not getting any errors and just cannot find any material on this topic.  Please, what am I doing wrong?  Thanks in advance...


    "If your parents didn't have any children, then neither will you" Benny Hill
    Friday, July 22, 2011 8:12 PM

All replies

  • It looks like the active selection in woird stays the same so the format will always be the same, unless I've missed something in my quick scan of the code?

    Rod Gill

    The one and only Project VBA Book Rod Gill Project Management
    Friday, July 22, 2011 11:46 PM
  • Okay, generally not being the sharpest tack in the box, how do I make it so the selection is at the spot where I am inserting my text?  I suppose that this would properly apply my formatting?  I really am very gratefull for any help, hints, or suggestions.  Is there an easier/better way of doing this?  Thanks again!
    "If your parents didn't have any children, then neither will you" Benny Hill
    Monday, July 25, 2011 1:57 PM