在delphi中xe 10.2下面代码的正确格式是什么 MSWord.Selection.ParagraphFormat.Alignment:= wdAlignParagraphJustify;
答案 0 :(得分:1)
以下代码说明了一种创建新Word文档的方法,即插入 将表格放入其中,然后指定单元格的对齐方式。
请注意,它有两次调用MSWord.Selection.ParagraphFormat.Alignment := wdAlignParagraphJustify
,
在创建表之后立即创建一个表,并在for
循环内设置另一个表来设置表
细胞。这是为了说明使用MSWord.Selection
的潜在问题,即Selection
可能不是您所期望的,因此对其进行操作会产生意外/不希望的结果。
如果您注释掉对MSWord.Selection.ParagraphFormat.Alignment := wdAlignParagraphJustify
的第二次通话
您会注意到只有第一个单元格才能获得该对齐。然后是对齐的
剩余的单元格将恢复到创建表格之前的状态。如果你注释掉第一个
相反,你会发现,就细胞的排列而言,它是超级的。
procedure TForm1.CreateTable(Rows, Columns : Integer);
var
MSWord,
Document,
Table,
Range,
Cell: OleVariant;
ARow,
AColumn: Integer;
RowIndex,
ColIndex : Integer;
S : String;
begin
MsWord := CreateOleObject('Word.Application');
MsWord.Visible := True;
Document := MSWord.Documents.Add;
MSWord.Selection.Font.Size := 22;
MSWord.Selection.Font.Bold := true;
MSWord.Selection.TypeText(#13#10);
MSWord.Selection.TypeText('This should be center-aligned');
MSWord.Selection.ParagraphFormat.Alignment := wdAlignParagraphCenter;
MSWord.Selection.TypeText(#13#10#13#10);
MSWord.Selection.Font.Size := 12;
MSWord.Selection.Font.Bold := False;
Table := MSWord.ActiveDocument.Tables.Add( Range:= MSWord.Selection.Range, NumRows:= Rows, NumColumns:= Columns, DefaultTableBehavior:= wdWord9TableBehavior, AutoFitBehavior:= wdAutoFitFixed);
MSWord.Selection.ParagraphFormat.Alignment := wdAlignParagraphJustify;
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;
MSWord.Selection.ParagraphFormat.Alignment := wdAlignParagraphJustify;
if Odd(AColumn) then
Range.Font.Bold := True
else
Range.Font.Bold := False;
S := Format('Row: %d, col: %d', [RowIndex, ColIndex]);
MSWord.Selection.Range := Range;
MSWord.Selection.TypeText(Text := S);
end;
end;
end;