0% found this document useful (0 votes)
15 views

Option Explicit

This function adds a table with the specified number of rows and columns to a Word document. It opens Word, creates a new document or opens an existing one, moves to the end, and inserts a table. It formats the table with grid lines and applies heading styles. The header row is formatted and populated with column headers.

Uploaded by

perica.trpkovski
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Option Explicit

This function adds a table with the specified number of rows and columns to a Word document. It opens Word, creates a new document or opens an existing one, moves to the end, and inserts a table. It formats the table with grid lines and applies heading styles. The header row is formatted and populated with column headers.

Uploaded by

perica.trpkovski
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

1. Option Explicit 2. 3. 'Add a reference to MS Word xx.0 Object Library 4. 5.

Public Function AddTable(ByVal iRows As Integer, ByVal iCols As Integer) As Boolean 6. 7. 8. 9. Dim oApp As Word.Application 10. 11. Dim oDoc As Word.Document 12. 13. Dim oTable As Word.Table 14. 15. Dim oRowHeader As Word.Row 16. 17. Dim x As Integer 18. 19. 20. 21. Set oApp = New Word.Application 22. 23. 'Open either a blank new document or... 24. 25. Set oDoc = oApp.Documents.Add 26. 27. 'open an exisiting document 28. 29. 'Set oDoc = oApp.Documents.Open("C:\Document1.doc") 30. 31. oDoc.Activate 32. 33. 'Move to the end of the document 34. 35. oApp.Selection.Move Unit:=wdStory 36. 37. oApp.Selection.TypeParagraph 38. 39. oApp.Selection.TypeParagraph 40. 41. 'Add a new table 42. 43. Set oTable = oDoc.Tables.Add(Range:=Selection.Range, NumRows:=iRows, NumColumns:=iCols, _ 44. 45. DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitWindow) 46. 47. 'Format the table and populate 48. 49. With oTable 50. 51. If .Style <> "Table Grid" Then 52. 53. .Style = "Table Grid" 54.

55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99.

End If .ApplyStyleHeadingRows = True .ApplyStyleLastRow = True .ApplyStyleFirstColumn = True .ApplyStyleLastColumn = True 'Populate and format the table header Set oRowHeader = oTable.Rows.Item(1) oRowHeader.Shading.Texture = wdTextureNone oRowHeader.Shading.ForegroundPatternColor = wdColorAutomatic oRowHeader.Shading.BackgroundPatternColor = wdColorGray25 For x = 1 To iCols 'Bold on for the cell .Cell(1, x).Range.Font.Bold = True .Cell(1, x).Range.Text = "ColumnHeader" & x Next End With 'Clean up and leave document open Set oRowHeader = Nothing Set oTable = Nothing Set oDoc = Nothing Set oApp = Nothing

End Function

You might also like