3

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

5
  • Are you sure your bookmark can't just be MOVED in your upstream document, solving this issue? Commented Jul 11, 2012 at 16:56
  • no matter where i put the bookmark it selects the title at the top of the page. I have added a section break still selects all, tried to add the title in front of the table programatically -but it ends up after the table no matter what I do Commented Jul 11, 2012 at 17:15
  • So what happens if you just remove the bookmark completely? Is the bookmark itself a red herring? Commented Jul 11, 2012 at 17:34
  • How else can I indicate to Delphi where to start creating the table if I don't use a bookmark? Commented Jul 11, 2012 at 19:08
  • I can't answer that question for you, but if you answer the question I asked, you might sort yourself out. Commented Jul 12, 2012 at 2:28

2 Answers 2

4

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;
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.