简体   繁体   English

从Delphi中在Word中创建表

[英]Create Table in Word from Delphi

I have some Delphi code that selects a bookmark in Word and then creates a table. 我有一些Delphi代码,在Word中选择一个书签,然后创建一个表。 My problem is that there is a title at the top of the page that when I select the range also gets selected and when my table is created the title is overwritten. 我的问题是页面顶部有一个标题,当我选择范围时也会被选中,当我的表被创建时,标题会被覆盖。 How can I just select the range after my bookmark to add my table so that my title is preserved? 如何在我的书签后选择范围来添加我的表格,以便保留我的标题?

R := WordApp.ActiveDocument.Bookmarks.Item('bmStartSecond').Range;
R.Select;
TableFormat(WordDoc, intCounter + 10);

function TableFormat(Adocument : variant; intNumRows : integer): variant;
var
wrdSelection: variant;
begin
  wrdSelection := WordApp.Selection;
  Adocument.Tables.Add(Range:=wrdSelection.Range, NumRows:=intNumRows, NumColumns:=3);
  Adocument.Tables.Item(1).Columns.Item(1).SetWidth(InchestoPoint(2.5),0);
  Adocument.Tables.Item(1).Columns.Item(2).SetWidth(InchestoPoint(2.25),0);
  Adocument.Tables.Item(1).Columns.Item(3).SetWidth(InchestoPoint(2.75),0);
  wordDoc.Tables.Item(1).Cell(Row:=1, Column:= 1).Range.Text := 'Offense:';
  wordDoc.Tables.Item(1).Cell(Row:=1, Column:= 2).Range.Text := 'Date & Place:';
  wordDoc.Tables.Item(1).Cell(Row:=1, Column:= 3).Range.Text := 'Disposition:';

  TableFormat := Adocument;
end;

Thanks, Leslie 谢谢,莱斯利

This adds a table after a named bookmark. 这会在命名书签后添加一个表。 You should be able to adapt it to your needs. 您应该能够根据您的需求进行调整。 (Your code is from decades ago, BTW - modern Delphi uses Result to indicate return values instead of FunctionName := . Result is an automatically-created variable of the proper type for the function.) Tested using Delphi 2007, Office XP components, on Windows 7 and an Office 2007 installation. (您的代码来自几十年前,BTW - 现代Delphi使用Result来指示返回值而不是FunctionName :=Result是一个自动创建的函数类型的变量。)使用Delphi 2007,Office XP组件进行测试Windows 7和Office 2007安装。

procedure TForm1.AddTable;
const
  Line1 = 'January,February,March';
  Line2 = '31,28,31';
  Line3 = '31,59,90';
var
  R, Direction, Separator, BookmarkName, TableFormat, Cols: OleVariant;
begin
  BookMarkName := 'bmTest';
  R := WordApp.ActiveDocument.Bookmarks.Item(BookmarkName).Range;
  Direction := wdCollapseEnd;
  R.Collapse(Direction);
  R.InsertAfter(Line1);
  R.InsertParagraphAfter;
  R.InsertAfter(Line2);
  R.InsertParagraphAfter;
  R.InsertAfter(Line3);
  R.InsertParagraphAfter;
  Separator := ',';
  TableFormat := wdTableFormatGrid1;
  R.ConvertToTable(Separator);

  // Cleaner to grab a reference to the table columns, and use
  // it instead of the long reference every time.
  Cols := WordApp.ActiveDocument.Tables.Item(1).Columns;
  Cols.Item(1).SetWidth(WordApp.InchesToPoints(2.25), wdAdjustNone);
  Cols.Item(2).SetWidth(WordApp.InchesToPoints(3.5), wdAdjustNone);
  Cols.Item(3).SetWidth(WordApp.InchesToPoints(2.75), wdAdjustNone);
end;

A function to createtable 一个可创建的功能

Function CreateTable(NumRows, NumColumns:integer;
var index:integer):boolean;
var sel_:variant;
begin
CreateTable:=true;
try
sel_:=W.selection;
W.ActiveDocument.Tables.Add (Range:=sel_.Range,NumRows: =NumRows,
  NumColumns:=NumColumns);
index:=W.ActiveDocument. Tables.Count;
except
CreateTable:=false;
end;
End;

and other to size a table: 和其他尺寸表:

Function SetSizeTable(Table:integer; RowsHeight,
ColumnsWidth:real):boolean;
begin
SetSizeTable:=true;
try
W.ActiveDocument.Tables.Item (Table).Columns.Width:=ColumnsWidth;
W.ActiveDocument.Tables.Item(Table). Rows.Height:=RowsHeight;
except
SetSizeTable:=false;
end;
End;

or set the size of a row / column 或设置行/列的大小

Function SetHeightRowTable(Table,Row:integer;
RowHeight:real):boolean;
begin
SetHeightRowTable:=true;
try
W.ActiveDocument.Tables.Item(Table).Rows.item(Row).Height:=RowHeight;
except
SetHeightRowTable:=false;
end;
End;
Function SetWidthColumnTable(Table,Column: integer;
ColumnWidth:real):boolean;
begin
SetWidthColumnTable:=true;
try
W.ActiveDocument.Tables.Item(Table).Columns.
  Item(Column).Width:=ColumnWidth;
except
SetWidthColumnTable:=false;
end;
End;

Here is more functions about word: http://delphimagic.blogspot.com.es/2013/03/funciones-para-trabajar-con-word.html 以下是关于单词的更多功能: http//delphimagic.blogspot.com.es/2013/03/funciones-para-trabajar-con-word.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM