简体   繁体   中英

Create Table in Word from Delphi

I have some Delphi code that selects a bookmark in Word and then creates a table. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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