I have difficulties understanding the Selection in Word Automation. To make it short, I want to have a table somewhere in my generated Word document, and I need to make alignments to the text inside a particular cell. How do I achieve that?
I use the ComObj approach:
var
MsWord: Variant;
...
try
MsWord := GetActiveOleObject('Word.Application');
except
try
MsWord := CreateOleObject('Word.Application');
MsWord.Visible := True;
except
Exception.Create('Error');
end;
end;
How do I tell Word where is the text (cell) to be able later to align it? I use this code to add text to a cell:
Function AddTextToTableCell(Table:integer;Row, Column:integer;text:string):boolean;
begin
AddTextToTableCell:=true;
try
MsWord.ActiveDocument.Tables.Item(Table).Columns.Item(Column).Cells.Item(Row).Range.Text:=text;
except
AddTextToTableCell:=false;
end;
End;
Can I somehow modify this to set the alignment of the text inside the cell? Let's say I want to center-align the cell located at: row:1 and column:3. My cell has multiple (variable) lines of text. I want to center it horizontally and vertically.
I searched other code of automation on Google and this line should do the trick, except it does not in delphi...(seems like VB equivalent works with it):
MSWORD.ActiveDocument.Tables.Item(Table).Cell(Row,Column).Range.Paragraphs.Alignment := wdAlignParagraphCenter;
If I use
MSWORD.ActiveDocument.Tables.Item(Table).Range.ParagraphFormat.Alignment := wdAlignParagraphCenter
then all the cells in the table are aligned to horiz-center. I only want one particular cell and I want it to be centered vertically too
Please help.
Thank you
I'm not sure if I'm misunderstanding your problem, but if I'm not, it's caused by a simple missing step. The following works for me (D7), with apologies if it seems a little long-winded, but I started out having the same problem as you:
procedure TForm1.Button1Click(Sender: TObject);
var
MSWord,
Document,
Table,
Selection,
Range,
Cell : OleVariant;
Rows,
Columns,
ARow,
AColumn : Integer;
RowIndex,
ColIndex : Integer;
S : String;
begin
try
MsWord := GetActiveOleObject('Word.Application');
except
try
MsWord := CreateOleObject('Word.Application');
MsWord.Visible := True;
except
Exception.Create('Error');
end;
end;
Rows := 3;
Columns := 4;
Document := MSWord.Documents.Add;
Table := MSWord.ActiveDocument.Tables.Add( Range:= MSWord.Selection.Range, NumRows:= Rows, NumColumns:= Columns, DefaultTableBehavior:= wdWord9TableBehavior, AutoFitBehavior:= wdAutoFitFixed);
for ARow := 1 to Rows do begin
for AColumn := 1 to Columns do begin
Cell := Table.Cell(ARow, AColumn);
RowIndex := Cell.RowIndex;
ColIndex := Cell.ColumnIndex;
Caption := IntToStr(RowIndex) + '/' + IntToStr(ColIndex);
Range := Cell.Range;
Range.Select;
S := Format('Row: %d, col: %d', [RowIndex, ColIndex]);
MSWord.Selection.Range := Range;
MSWord.Selection.TypeText(Text := S);
end;
end;
Cell := Table.Cell(2, 2);
Range := Cell.Range;
Range.Select;
Range.ParagraphFormat.Alignment := wdAlignParagraphCenter;
end;
The key thing is the "range.select"s. If you comment those out, you'll find that all the text ends up in the 1,1 cell.
I assume you have already write some complex codes and would prefer not to rewrite you code. I do however want to advise you to look at TMS Software at enter link description here . They have very rich table components.
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.