Excel VBA Macro: Create Word Document & Add Table (Static Range)

preview_player
Показать описание
Excel VBA Macro: Create Word Document & Add Table (Static Range). In this video, we go over how to add a table with a set range to a word document using excel. We also enable borders in the table and change the background color of the table with our code.

Code:

Sub add_table_2_word()

Dim objWord
Dim objDoc
Dim objSelection
Dim i As Integer
Dim j As Integer

Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add
Set objSelection = objWord.Selection

objWord.Visible = True
objWord.Activate

Set CountryTable = objDoc.Tables.Add(objSelection.Range, 6, 3)

With CountryTable

With .Borders
.enable = True
.outsidecolor = RGB(0, 0, 0)
.insidecolor = RGB(0, 0, 0)
End With


For i = 1 To 6

For j = 1 To 3

.cell(i, j).Range.InsertAfter ThisWorkbook.Sheets("Sheet1").Cells(i, j).Text

Next j

Next i

End With

End Sub

#ExcelVBA #ExcelMacro
Рекомендации по теме
Комментарии
Автор

Don't forget to close the word file once macro gets executed otherwise(for me at least) it causes a bug ! Good job Greggo

Lyriks_
Автор

Thanks for a educative lesson .... how can I get auto serial number in word table when I extract data from excelsheet ??

sallum
Автор

Thanks. Cant you get the _named_ table from excel and paste it to word?

stanTrX
Автор

Can you use the same method to make an Equation?
If yes, then how can you do it?

johanmilterjakobsen
Автор

How can we add the table in specific place in a word template ?

manoharbholankar
Автор

How to filter out with multiple criteria for a column, could you please explain me

vikrampurohit
welcome to shbcf.ru