OLE 自动化 - WORD 表格 (Delphi)
我正在尝试在 WORD 中的表内制作表格。 当然,在最终程序中它将是动态的,这不在本示例中。
这是我的示例代码。
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);
首先,我将新表(2行,1列)放在光标的位置上,然后尝试将第二个表放入第一个表的单元格(1,1)中,将第三个表放入第一个表的单元格(2,1)中。 第二个表也有 2 行和 1 列,但第三个表有 3 行和 1 列。 但我得到的不是我想要的,而是只有一行的第二个和第三个表,无论我是否在单元格中放入了某些内容。我总是只看到我放入该表中的最后一个字符串。
更重要的是,如果我将 1 行和 2 列表放入第一个表中,则一切正常。
你能帮助我吗。
谢谢,洛克
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您在代码中创建这些表格时遇到问题,请执行以下操作:
When you have problems creating those tables in code, do the following:
您必须对 bTable & 执行相同的操作。 c表。
当您添加超过 1 行/列时,将需要边框将其分隔开(即,将 1 行与另一行分隔开或将 1 列与另一列分隔开)。
希望这可以帮助。
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.