Sub FnAddTables()
Dim arrData
Dim arrNumData
Dim cell As Range
Dim intNoOfColumns
Dim objWord
Dim objDoc
Dim ClientName As String
Dim Clientname1 As String
Dim Text As String
arrData = Range("B5:G26")
intNoOfColumns = 6
'Open tax planning template document
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Open("C:\Users\Huis\Desktop\Chandoo Example.docx") 'this will need amending accordingly
'Add client name to the front cover
ClientName = ActiveWorkbook.Sheets("Sheet1").Range("C3")
With objDoc
If objDoc.Bookmarks.Exists("ClientName") Then
.Bookmarks("ClientName").Range.Text = ClientName
End If
End With
'Move to the end of the document
objDoc.Characters.Last.Select
'Apply styling to text
objDoc.Bookmarks.Add ("ClientName1")
Clientname1 = ActiveWorkbook.Sheets("Sheet1").Range("C3")
With objDoc
If objDoc.Bookmarks.Exists("ClientName1") Then
.Bookmarks("ClientName1").Range.Text = Clientname1
objWord.Selection.Style = -2
End If
End With
objDoc.Characters.Last.Select
objWord.Selection.InsertParagraphAfter
objDoc.Characters.Last.Select
'addchar ' what is going here?
'
'Add Estimated Net Profit Before Tax table
'
Set objRange = objDoc.Range
objDoc.Tables.Add objRange, 1, intNoOfColumns
Set objTable = objDoc.Tables(1)
objTable.Borders.Enable = False
'Add data into table
For i = 1 To UBound(arrData)
If arrData(i, 1) <> "" Then
If i > 1 Then objTable.Rows.Add
For j = 1 To intNoOfColumns
objTable.cell(i, j).Range.Text = arrData(i, j)
Next
End If
Next
'Format style = No Spacing
objTable.Range.Style = "No Spacing"
'Right justify cells from Cell(1, 2) to (i, j)
numCell = objTable.Rows.Count
If numCell > 1 Then
For i = 1 To numCell
For j = 2 To intNoOfColumns
objTable.cell(i, j).Range.ParagraphFormat.Alignment = 2
Next
Next
End If
'Format cells from Cell(2, 2) to (i, j) to #,##0
If numCell > 1 Then
For i = 2 To numCell
For j = 2 To intNoOfColumns
objTable.cell(i, j).Range.Text = VBA.Format(Val(objTable.cell(i, j).Range.Text), "#,##0")
Next
Next
End If
'Format "Total" borders
If numCell > 1 Then
For i = numCell To numCell
For j = 2 To intNoOfColumns
objTable.cell(i, j).Range.Borders(wdBordertop).LineStyle = wdLineStyleSingle
objTable.cell(i, j).Range.Borders(wdBorderBottom).LineStyle = wdLineStyleDouble
Next
Next
End If
'Format row 1 of table "Bold"
objTable.Rows(1).Range.Font.Bold = True
objTable.Rows(objTable.Rows.Count).Range.Font.Bold = True
'Autofit column widths
objTable.Columns.AutoFit
'
'Clear out data ready for next table
'
objWord.ActiveDocument.Characters.Last.Select
objWord.Selection.Collapse
With objRange
.Collapse Direction:=wdCollapseEnd
.MoveEnd
.InsertParagraphAfter
.Collapse Direction:=wdCollapseEnd
End With
objDoc.Characters.Last.Select
objWord.Selection.InsertParagraphAfter
objDoc.Characters.Last.Select
Text = ActiveWorkbook.Sheets("Sheet1").Range("K6")
objWord.Selection.TypeText (Text)
objWord.Selection.InsertParagraphAfter
objDoc.Characters.Last.Select
'
'Bullet points start here
'
Text = ActiveWorkbook.Sheets("Sheet1").Range("K7")
objWord.Selection.TypeText (Text)
'objDoc.Selection.Range.ListFormat.ApplyListTemplateWithLevel _
ListTemplate:=ListGalleries(wdBulletGallery).ListTemplates(1), _
ContinuePreviousList:=False, ApplyTo:=wdListApplyToWholeList, _
DefaultListBehavior:=wdWord10ListBehavior
objWord.Selection.InsertParagraphAfter
objDoc.Characters.Last.Select
Text = ActiveWorkbook.Sheets("Sheet1").Range("K8")
objWord.Selection.TypeText (Text)
objWord.Selection.InsertParagraphAfter
objDoc.Characters.Last.Select
Text = ActiveWorkbook.Sheets("Sheet1").Range("K9")
objWord.Selection.TypeText (Text)
objWord.Selection.InsertParagraphAfter
objDoc.Characters.Last.Select
Text = ActiveWorkbook.Sheets("Sheet1").Range("K10")
objWord.Selection.TypeText (Text)
objWord.Selection.InsertParagraphAfter
objDoc.Characters.Last.Select
Text = ActiveWorkbook.Sheets("Sheet1").Range("K11")
objWord.Selection.TypeText (Text)
objWord.Selection.InsertParagraphAfter
objDoc.Characters.Last.Select
'
'Bullet points end here
'
Text = ActiveWorkbook.Sheets("Sheet1").Range("K12")
objWord.Selection.TypeText (Text)
objWord.Selection.InsertParagraphAfter
objDoc.Characters.Last.Select
objWord.Visible = True
End Sub