OLE MSWord - tables

I have some code that creates an MSWord object and populates it with data. The data is either put in as straightforward text along the left side if the Word document. But some data needs to be put into tables. The number of tables are dynamic depending on the number if notes documents in a collection. I have code to create the tables but when it goes to putting the 2nd table of the report, it puts it in the first cell of the 1st table. I think it has something to do with the range specified but I don’t know OLE very well and don’t know how the range works. Here is a sample of the code:

Set WordObj = CreateObject(“Word.Application”)

WordObj.visible = True

Set WordDocs = WordObj.Documents

WordDocs.Add

Set WordDoc = WordObj.ActiveDocument

Set range = WordDoc.Range(0,0)



Set tableobj = WordObj.selection.Tables.Add(range,3,3)

Call tableobj.cell(1,1).select 

Call wordobj.selection.typetext(Cstr(Today)) 

Call tableobj.cell(1,2).select 

Call wordobj.selection.typetext("name")

Call tableobj.cell(1,3).select 

Call wordobj.selection.typetext("Page 1")

Call tableobj.cell(2,1).select 

Call wordobj.selection.typetext("")

Call tableobj.cell(2,2).select 

Call wordobj.selection.typetext("Activity Report")

Call tableobj.cell(2,3).select 

Call wordobj.selection.typetext("")

Call tableobj.cell(3,1).select 

Call wordobj.selection.typetext("")

Call tableobj.cell(3,2).select 

Call wordobj.selection.typetext(doc.SeedRep(0))

Call tableobj.cell(3,3).select 

Call wordobj.selection.typetext("")

tblcount=tblcount+1

WordObj.selection.MoveDown

.

.

.

' Call range.MoveEnd

		WordObj.Application.Selection.TypeParagraph

		Set tableobj = WordObj.selection.Tables.Add(range,4,2)

		Call tableobj.cell(1,1).select 

		Call wordobj.selection.typetext("Volume Discount:") 

		Call tableobj.cell(1,2).select 

		Call wordobj.selection.typetext(Cstr(doc4.DMVOLD(0)))

		Call tableobj.cell(2,1).select 

		Call wordobj.selection.typetext("Early Order Units:") 

		Call tableobj.cell(2,2).select 

		Call wordobj.selection.typetext(Cstr(doc4.DMEODQ(0)))

		Call tableobj.cell(3,1).select 

		Call wordobj.selection.typetext("Sub Balance Due:") 

		Call tableobj.cell(3,2).select 

		Call wordobj.selection.typetext(Cstr(doc4.DMDUE(0)))

		custtotaldue=custtotaldue+doc4.DMDUE(0)

		Call tableobj.cell(4,1).select 

		Call wordobj.selection.typetext("Joint Balance Due:") 

		Call tableobj.cell(4,2).select 

		Call wordobj.selection.typetext(Cstr(doc4.DMJDUE(0)))

		custjointtotal=custjointtotal+Cdbl(doc4.DMJDUE(0))

I need the cursor/pointer to get to the end of the selection/range(bottom of the word doc) before creating the next table. Do I need an array of table objects? Any help is greatly appreciated.

Subject: OLE MSWord - tables

I’ve had the same problem and I found a workaround, but I suspect that it should be another more proper way to do it.

I added the following two lines before adding another table:

objWord.Selection.InsertRowsBelow 1

objWord.Selection.MoveEnd 5, 2

hth