Table from Excel to Word Bookmark VBA

rakesh seebaruth

Active Member
Joined
Oct 6, 2011
Messages
303
hi Guys

A table is being inserted from Excel to Word.It populates the table by rows and columns. Vba opens my file in word .draw and fill the table in my word document. The main issue i am having is that despite i have inserted a bookmark in my word document the table is not inserted at the bookmark's place. My codes are as follows :-
VBA Code:
Sub CreateTableInWord()

Dim objWord As Object, objDoc As Object, objTbl As Object, objRow As Object
Dim objCol As Object, colSets As Long, numMonths As Long, i As Long, n As Long, c As Long
Dim amt, dtStart, tblRows As Long, tblCols As Long, rw As Long, col As Long

numMonths = Range("A1").Value
amt = Range("B1").Value
dtStart = Range("C1").Value
colSets = Range("D1").Value 'how many sets of columns ?

tblRows = 1 + Application.Ceiling(numMonths / colSets, 1) 'how many table rows?
tblCols = colSets * 3 'how many table cols?

Set objWord = CreateObject("Word.Application")
objWord.Visible = True
'Set objDoc = objWord.Documents.Add
Set objDoc = objWord.Documents.Open("C:\Users\rakesh\Desktop\mailmerge\lease2.docx")

Dim oRange As Object
Set oRange = objDoc.Bookmarks("RS").Range

Set objTbl = objDoc.Tables.Add(Range:=objDoc.Paragraphs(1).Range, _
NumRows:=tblRows, NumColumns:=tblCols)

c = 0
For n = 1 To colSets
objTbl.Cell(1, c + 1).Range.Text = "Instal No"
objTbl.Cell(1, c + 1).Range.Bold = True
objTbl.Cell(1, c + 2).Range.Text = "Amt(Rs)"
objTbl.Cell(1, c + 2).Range.Bold = True
objTbl.Cell(1, c + 3).Range.Text = "Due Date"
objTbl.Cell(1, c + 3).Range.Bold = True
c = c + 3
Next n
objTbl.Range.ParagraphFormat.Alignment = 1 ' wdAlignParagraphCenter

rw = 2
col = 0
For i = 1 To numMonths

'rw = 1 + Application.Ceiling(i / colSets, 1) 'fill across and then down
rw = IIf(i Mod (tblRows - 1) = 1, 2, rw + 1) 'fill down then across

objTbl.Cell(rw, col + 1).Range.Text = i
objTbl.Cell(rw, col + 2).Range.Text = amt
objTbl.Cell(rw, col + 3).Range.Text = Format(DateAdd("m", i - 1, dtStart), "dd/mm/yyyy")



'col = IIf(i Mod colSets = 0, 0, col + 3) 'fill across and then down
col = IIf(i Mod (tblRows - 1) = 0, col + 3, col) 'fill down and then across

Next i


End Sub


Your help will be highly appreciated

Thanks/regards

rakesh
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Change:
VBA Code:
Set objTbl = objDoc.Tables.Add(Range:=objDoc.Paragraphs(1).Range, NumRows:=tblRows, NumColumns:=tblCols)
to:
Rich (BB code):
Set objTbl = objDoc.Tables.Add(Range:=oRange, NumRows:=tblRows, NumColumns:=tblCols)
 
Upvote 0

Forum statistics

Threads
1,214,805
Messages
6,121,665
Members
449,045
Latest member
Marcus05

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top