How it is possible for every "main", "sub" and "sub-sub" Cases set number before Heading. Numbers should be entered starting from one.
For numbering I guess it possible to apply Numbering for "Heading 2" at the end of the code something like `objDoc.Range.Font.ColorIndex = wdBlack`? What code should I use for that? I have been exploring internet for 2 hours and couldn't find any suitable.
Here is my full code:
https://stackoverflow.com/questions...nt-bold?noredirect=1#comment95634132_54406888
Code:
Main -> 1
Main -> 2
Sub -> 2.1
Sub -> 2.2
Sub-Sub -> 2.2.1
Sub-Sub -> 2.2.2
Sub-Sub -> 2.2.3
Main -> 3
Main -> 4
Sub -> 4.1
Main -> `.Font.Bold = True`
Sub -> `.Font.Bold = True`
Here is my full code:
Code:
Option Explicit
Sub main()
Dim objWord As Object
Dim objDoc As Object
Dim objSelection As Object
Dim cell As Range
Set objWord = CreateObject("Word.Application") '<--| get a new instance of Word
Set objDoc = objWord.Documents.Add '<--| add a new Word document
objWord.Visible = True
Set objSelection = objDoc.ActiveWindow.Selection '<--| get new Word document 'Selection' object
With objSelection '<--| reference 'Selection' object
For Each cell In ThisWorkbook.Worksheets("Offer Letter").Range("C1", ThisWorkbook.Worksheets("Offer Letter").Range("C" & Rows.Count).End(xlUp))
Select Case LCase(cell.Value)
Case "title"
.TypeParagraph
.Style = objWord.ActiveDocument.Styles("Heading 1")
'.ActiveDocument.Font.Bold = True
.TypeText Text:=cell.Offset(0, -1).Text
.ParagraphFormat.SpaceAfter = 20
.ParagraphFormat.SpaceBefore = 20
Case "main"
.TypeParagraph
.Style = objWord.ActiveDocument.Styles("Heading 2")
.TypeText Text:=cell.Offset(0, -1).Text
Case "sub"
.TypeParagraph
.Style = objWord.ActiveDocument.Styles("Heading 2")
.TypeText Text:=cell.Offset(0, -1).Text
Case "sub-sub"
.TypeParagraph
.Style = objWord.ActiveDocument.Styles("Heading 2")
.TypeText Text:=cell.Offset(0, -1).Text
Case "contact"
.TypeParagraph
.Style = objWord.ActiveDocument.Styles("Normal")
.TypeText Text:=cell.Offset(0, -1).Text
.ParagraphFormat.SpaceAfter = 0
Case "par"
.TypeParagraph
.Style = objWord.ActiveDocument.Styles("Normal")
.TypeText Text:=cell.Offset(0, -1).Text
Case "attachment"
.TypeParagraph
.Style = objWord.ActiveDocument.Styles("Normal")
.TypeText Text:=cell.Offset(0, -1).Text
.ParagraphFormat.SpaceAfter = 0
End Select
Next cell
objDoc.Range.Font.Name = "Arial"
objDoc.Range.Font.ColorIndex = wdBlack
End With
objDoc.SaveAs2 ActiveWorkbook.Path & "\" & Sheets("Other Data").Range("AN2").Value & ", " & Sheets("Other Data").Range("AN7").Value & "_" & Sheets("Other Data").Range("AN8").Value & "_" & Sheets("Other Data").Range("AX2").Value & ".docx" '<--| save your word document
objWord.Quit '<--| quit Word
Set objWord = Nothing '<--| release object variable
End Sub
https://stackoverflow.com/questions...nt-bold?noredirect=1#comment95634132_54406888