Sub Demo()
' Note: A reference to the Microsoft Word # Object Library is required,
' set via Tools|References in the Excel VBE.
Dim WdApp As New Word.Application, WdDoc As Word.Document, XlSht As Excel.Worksheet
Dim lRow As Long, r As Long, h As Long, StrHd As String, StrTxt As String
Set XlSht = ThisWorkbook.Worksheets("Sheet1")
lRow = XlSht.UsedRange.Cells.SpecialCells(xlCellTypeLastCell).Row
With WdApp
Set WdDoc = .Documents.Add
Call ApplyMultiLevelHeadingNumbers(WdDoc)
For r = 2 To lRow
h = UBound(Split(XlSht.Cells(r, 1).Text, ".")) + 1
StrHd = XlSht.Cells(r, 2).Text
StrTxt = XlSht.Cells(r, 3).Text
With WdDoc
.Paragraphs.Last.Style = "Heading " & h
.Paragraphs.Last.Range.Text = StrHd
.Range.InsertAfter vbCr
.Paragraphs.Last.Style = wdStyleNormal
If StrTxt <> "" Then
.Paragraphs.Last.Range.Text = StrTxt
.Range.InsertAfter vbCr
End If
End With
Next
With WdDoc
Do While .Characters.Last.Previous = vbCr
.Characters.Last.Previous.Delete
Loop
End With
.Visible = True
End With
WdDoc.Activate
Set WdDoc = Nothing: Set WdApp = Nothing: Set XlSht = Nothing
End Sub
Sub ApplyMultiLevelHeadingNumbers(WdDoc As Word.Document)
Dim LT As ListTemplate, i As Long
Set LT = WdDoc.ListTemplates.Add(OutlineNumbered:=True)
For i = 1 To 9
With LT.ListLevels(i)
.NumberFormat = Choose(i, "%1", "%1.%2", "%1.%2.%3", "%1.%2.%3.%4", "%1.%2.%3.%4.%5", "%1.%2.%3.%4.%5.%6", "%1.%2.%3.%4.%5.%6.%7", "%1.%2.%3.%4.%5.%6.%7.%8", "%1.%2.%3.%4.%5.%6.%7.%8.%9")
.TrailingCharacter = wdTrailingTab
.NumberStyle = wdListNumberStyleArabic
.NumberPosition = CentimetersToPoints(0)
.Alignment = wdListLevelAlignLeft
.TextPosition = InchesToPoints(0.5 + i * 0.5)
.ResetOnHigher = True
.StartAt = 1
.LinkedStyle = "Heading " & i
End With
With WdDoc.Styles("Heading " & i)
.ParagraphFormat.LeftIndent = InchesToPoints(i * 0.5 - 0.5)
.ParagraphFormat.FirstLineIndent = InchesToPoints(-0.5)
.Font.Name = "Gill Sans MT"
.Font.Italic = False
.Font.Bold = False
.Font.ColorIndex = wdAuto
.Font.Size = 17 - i
End With
Next
End Sub