繁体   English   中英

从Delphi中在Word中创建表

[英]Create Table in Word from Delphi

我有一些Delphi代码,在Word中选择一个书签,然后创建一个表。 我的问题是页面顶部有一个标题,当我选择范围时也会被选中,当我的表被创建时,标题会被覆盖。 如何在我的书签后选择范围来添加我的表格,以便保留我的标题?

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;

谢谢,莱斯利

这会在命名书签后添加一个表。 您应该能够根据您的需求进行调整。 (您的代码来自几十年前,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;

一个可创建的功能

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;

和其他尺寸表:

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;

或设置行/列的大小

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;

以下是关于单词的更多功能: 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