OLE 自动化 - WORD 表格 (Delphi)

发布于 2024-07-27 17:05:57 字数 1634 浏览 5 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

阳光的暖冬 2024-08-03 17:05:57

当您在代码中创建这些表格时遇到问题,请执行以下操作:

  • 打开 Word
  • 录制新宏
  • 录制时,构建所需的表格,然后停止录制。
  • 在 Visual Basic 编辑器中查看宏代码并尝试将其转换为 OLE 自动化代码(这并不难,几乎是一样的)

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)
呆头 2024-08-03 17:05:57
aTable.Borders.Item(wdBorderVertical).LineStyle:=wdLineStyleSingle;
aTable.Borders.Item(wdBorderHorizontal).LineStyle:=wdLineStyleSingle;

您必须对 bTable & 执行相同的操作。 c表。

当您添加超过 1 行/列时,将需要边框将其分隔开(即,将 1 行与另一行分隔开或将 1 列与另一列分隔开)。

希望这可以帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文