2
votes

I'm trying to make tables inside tables in WORD. of course in finall program it will be dinamical, which is not in this sample.

Here is my sample code.

 var
  aTable, bTable, cTable : OLEVariant;
begin
  m_WordApplication := CreateOleObject('Word.Application') ;
  m_WordDocument := m_WordApplication.Documents.Add;

  aTable := m_WordDocument.Tables.Add(m_WordApplication.Selection.Range, 2, 1);
  aTable.Borders.Item(wdBorderLeft).LineStyle:=wdLineStyleSingle;
  aTable.Borders.Item(wdBorderRight).LineStyle:=wdLineStyleSingle;
  aTable.Borders.Item(wdBorderTop).LineStyle:=wdLineStyleSingle;
  aTable.Borders.Item(wdBorderBottom).LineStyle:=wdLineStyleSingle;

  bTable := m_WordDocument.Tables.Add(aTable.Cell(1, 1).Range, 2, 1);
  bTable.Borders.Item(wdBorderLeft).LineStyle:=wdLineStyleSingle;
  bTable.Borders.Item(wdBorderRight).LineStyle:=wdLineStyleSingle;
  bTable.Borders.Item(wdBorderTop).LineStyle:=wdLineStyleSingle;
  bTable.Borders.Item(wdBorderBottom).LineStyle:=wdLineStyleSingle;

  cTable := m_WordDocument.Tables.Add(aTable.Cell(2, 1).Range, 3, 1);
  cTable.Borders.Item(wdBorderLeft).LineStyle:=wdLineStyleSingle;
  cTable.Borders.Item(wdBorderRight).LineStyle:=wdLineStyleSingle;
  cTable.Borders.Item(wdBorderTop).LineStyle:=wdLineStyleSingle;
  cTable.Borders.Item(wdBorderBottom).LineStyle:=wdLineStyleSingle;

  m_WordDocument.SaveAs('C:/test.doc', False) ;
  m_WordApplication.Quit(False);

Firstly i put new table(2 rows, 1 column) on position of the cursor, and then i try to put second table in cell(1,1) and third in cell(2,1) of the first table. second table has also 2 rows and 1 column, but third table has 3 rows and 1 column. but instead of what i want i get second and third table whit only one row, regardless if i putt something in thier cell or not.i always see only the last string i put in that table.

even more, if i put 1 row and 2 column table inside first table, than everything is normal.

can you help me.

thanks, Rok

2
Perhaps you can edit your post to make it more readable. (Most of all: proper spelling)Daniel Rikowski
Also I strongly suggest to use early binding (= use the interfaces from the imported Word_TLB), which has a number of advantages over late binding like this (= using OleVariants)Stijn Sanders

2 Answers

3
votes

When you have problems creating those tables in code, do the following:

  • Open Word
  • record a new macro
  • While recording, build the table you want, then stop the recording.
  • View your macro code in the Visual Basic Editor and try to translate that to OLE-automation code (which isn't that hard, it's almost the same)
0
votes
aTable.Borders.Item(wdBorderVertical).LineStyle:=wdLineStyleSingle;
aTable.Borders.Item(wdBorderHorizontal).LineStyle:=wdLineStyleSingle;

You will have to do the same for bTable & cTable.

When you add more than 1 row/column, it will need border to separate it (i.e to separate 1 row from another OR separate 1 column from another).

Hope this helps.