Hi ALL
I actually need your help on modifying this macro code. I got this code from somewhere else (sorry, forgot his name!). Basically, I have table data source from Sheet1 of excel. I need to transfer this table, keeping the formatting like borders, font size, auto fit, etc to MS Word with my bookmark named 'bookmark'.
Something to note:
1. Bookmark's location is at the VERY LAST PARAGRAPH. I have more than 12 paragraphs in my Word file but the table should be placed at the very last paragraph.
2. Please note the number of rows and columns in the table data source is dynamic. This means, the number of rows and columns are not fixed. It can change. My existing macro code works for this. However, it entirely deleted the existing paragraphs. The imported table replaced everything on my file.
Appreciate your help on item 2. Below is the code. Thank you very much in advance!
******************************
<>
Private Sub CommandButton1_Click()
On Error Resume Next
' FIRST GET THE ROWS COLUMNS OF A USED RANGE.
Dim iTotalRows As Integer ' GET TOTAL USED RANGE ROWS.
iTotalRows = Worksheets("sheet1").UsedRange.Rows.Count
Dim iTotalCols As Integer ' GET TOTAL COLUMNS.
iTotalCols = Worksheets("sheet1").UsedRange.Columns.Count
' WORD OBJECT.
Dim oWord As Object
Set oWord = CreateObject(Class:="Word.Application")
oWord.Visible = True
oWord.Activate
' ADD A DOCUMENT TO THE WORD OBJECT.
Dim oDoc
Set oDoc = oWord.Documents.Open("C:\Macro\samplebookmark1.docx")
' CREATE A RANGE FOR THE TABLE INSIDE WORD DOCUMENT.
Dim oRange
Set oRange = oDoc.Range
' CREATE AND DEFINE TABLE STRUCTURE USING
' THE ROWS AND COLUMNS EXTRACTED FROM EXCEL USED RANGE.
oDoc.Tables.Add oRange, iTotalRows, iTotalCols
' CREATE A TABLE OBJECT.
Dim oTable
Set oTable = oDoc.Tables(1)
oTable.Borders.Enable = True ' YES, WE WANT BORDERS.
Dim iRows, iCols As Integer
' LOOP THROUGH EACH ROW AND COLUMN TO EXTRACT DATA IN EXCEL.
For iRows = 1 To iTotalRows
For iCols = 1 To iTotalCols
Dim txt As Variant
txt = Worksheets("Sheet1").Cells(iRows, iCols)
oTable.cell(iRows, iCols).Range.Text = txt ' COPY (OR WRITE) DATA TO THE TABLE.
' BOLD HEADERS.
If Val(iRows) = 1 Then
objTable.cell(iRows, iCols).Range.Font.Bold = True
End If
Next iCols
Next iRows
Set oWord = Nothing
End Sub
<>
I actually need your help on modifying this macro code. I got this code from somewhere else (sorry, forgot his name!). Basically, I have table data source from Sheet1 of excel. I need to transfer this table, keeping the formatting like borders, font size, auto fit, etc to MS Word with my bookmark named 'bookmark'.
Something to note:
1. Bookmark's location is at the VERY LAST PARAGRAPH. I have more than 12 paragraphs in my Word file but the table should be placed at the very last paragraph.
2. Please note the number of rows and columns in the table data source is dynamic. This means, the number of rows and columns are not fixed. It can change. My existing macro code works for this. However, it entirely deleted the existing paragraphs. The imported table replaced everything on my file.
Appreciate your help on item 2. Below is the code. Thank you very much in advance!
******************************
<>
Private Sub CommandButton1_Click()
On Error Resume Next
' FIRST GET THE ROWS COLUMNS OF A USED RANGE.
Dim iTotalRows As Integer ' GET TOTAL USED RANGE ROWS.
iTotalRows = Worksheets("sheet1").UsedRange.Rows.Count
Dim iTotalCols As Integer ' GET TOTAL COLUMNS.
iTotalCols = Worksheets("sheet1").UsedRange.Columns.Count
' WORD OBJECT.
Dim oWord As Object
Set oWord = CreateObject(Class:="Word.Application")
oWord.Visible = True
oWord.Activate
' ADD A DOCUMENT TO THE WORD OBJECT.
Dim oDoc
Set oDoc = oWord.Documents.Open("C:\Macro\samplebookmark1.docx")
' CREATE A RANGE FOR THE TABLE INSIDE WORD DOCUMENT.
Dim oRange
Set oRange = oDoc.Range
' CREATE AND DEFINE TABLE STRUCTURE USING
' THE ROWS AND COLUMNS EXTRACTED FROM EXCEL USED RANGE.
oDoc.Tables.Add oRange, iTotalRows, iTotalCols
' CREATE A TABLE OBJECT.
Dim oTable
Set oTable = oDoc.Tables(1)
oTable.Borders.Enable = True ' YES, WE WANT BORDERS.
Dim iRows, iCols As Integer
' LOOP THROUGH EACH ROW AND COLUMN TO EXTRACT DATA IN EXCEL.
For iRows = 1 To iTotalRows
For iCols = 1 To iTotalCols
Dim txt As Variant
txt = Worksheets("Sheet1").Cells(iRows, iCols)
oTable.cell(iRows, iCols).Range.Text = txt ' COPY (OR WRITE) DATA TO THE TABLE.
' BOLD HEADERS.
If Val(iRows) = 1 Then
objTable.cell(iRows, iCols).Range.Font.Bold = True
End If
Next iCols
Next iRows
Set oWord = Nothing
End Sub
<>