views:

1121

answers:

4

Hi,

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

A: 
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.

shahkalpesh
A: 

unfortunately, this does not work. and it would be very strange if it would help, cause style shouldn't interfere in functunality of code.

Rok
StackOverflow is not a forum. Update your question if you want to provide more information or comment an existing answer.
DR
Or add a comment to the answer.
gabr
+2  A: 

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)
The_Fox
A: 

The_Fox: i can not belive that MicroSoft has something useful. I owe you man.

Rok