一个例子 里面含有对 Word 操作的常用方法:定位替换、表格等。 procedure TForm1.Button1Click(Sender: TObject); var WordApp,MyDoc,wTable: OLEVariant; FileName: string; i: integer; procedure FillRow(wTable: OLEVariant; Row,StartColumn: Byte; ss: array of String); var j: word; begin for j := Low(ss) to High(ss) do begin wTable.Cell(Row,j+StartColumn).Range.Text := ss[j]; end; end; begin FileName := ExtractFileDir(Application.ExeName) + '\test1.dot'; if not FileExists(FileName) then raise Exception.Create('找不到模板!'); try WordApp := CreateOleObject('Word.Application'); MyDoc := CreateOleObject('Word.Document'); except Raise Exception.Create('无法打开Word,请确认已安装Word。'); end; WordApp.Visible := true; MyDoc := WordApp.Documents.Add(FileName,false); MyDoc.BookMarks.Item('表名1').Select; WordApp.Selection.Text := 'Animals'; wTable := MyDoc.Tables.Item(1); table1.First; for i := 2 to 4 do begin if not Table1.Eof then begin FillCell(i,1,wtable,'Name'); FillCell(i,2,wtable,'Size'); FillCell(i,3,wtable,'Area'); Table1.Next end else Break end; wTable := MyDoc.Tables.Item(2); table1.First; for i := 1 to 3 do if not table1.Eof then begin FillRow(wTable,i,2,[table1.fieldbyName('Name').AsString, table1.FieldByName('Size').AsString, table1.FieldByName('Area').AsString]); table1.Next end; MyDoc.Tables.Add(MyDoc.Books.Item('表格3').Range,2,5) end;procedure TForm1.FillCell(i, j: byte; wTable: Variant; FieldName: String); begin wTable.Cell(i,j).Range.Text := table1.FieldByName(FieldName).AsString end;
procedure TForm1.FillCell(i, j: byte; wTable: Variant; FieldName: String);
begin
wTable.Cell(i,j).Range.Text := table1.FieldByName(FieldName).AsString
end;
你写个逆过程就行了
你的意思我不明白。
里面含有对 Word 操作的常用方法:定位替换、表格等。
procedure TForm1.Button1Click(Sender: TObject);
var WordApp,MyDoc,wTable: OLEVariant;
FileName: string;
i: integer;
procedure FillRow(wTable: OLEVariant; Row,StartColumn: Byte; ss: array of String);
var j: word;
begin
for j := Low(ss) to High(ss) do begin
wTable.Cell(Row,j+StartColumn).Range.Text := ss[j];
end;
end;
begin
FileName := ExtractFileDir(Application.ExeName) + '\test1.dot';
if not FileExists(FileName) then
raise Exception.Create('找不到模板!');
try
WordApp := CreateOleObject('Word.Application');
MyDoc := CreateOleObject('Word.Document');
except
Raise Exception.Create('无法打开Word,请确认已安装Word。');
end; WordApp.Visible := true;
MyDoc := WordApp.Documents.Add(FileName,false); MyDoc.BookMarks.Item('表名1').Select;
WordApp.Selection.Text := 'Animals';
wTable := MyDoc.Tables.Item(1);
table1.First;
for i := 2 to 4 do begin
if not Table1.Eof then begin
FillCell(i,1,wtable,'Name');
FillCell(i,2,wtable,'Size');
FillCell(i,3,wtable,'Area');
Table1.Next
end else Break
end; wTable := MyDoc.Tables.Item(2);
table1.First;
for i := 1 to 3 do
if not table1.Eof then begin
FillRow(wTable,i,2,[table1.fieldbyName('Name').AsString,
table1.FieldByName('Size').AsString,
table1.FieldByName('Area').AsString]);
table1.Next
end; MyDoc.Tables.Add(MyDoc.Books.Item('表格3').Range,2,5)
end;procedure TForm1.FillCell(i, j: byte; wTable: Variant; FieldName: String);
begin
wTable.Cell(i,j).Range.Text := table1.FieldByName(FieldName).AsString
end;
wTable := MyDoc.Tables.Item(1);
:)
很多的
都是单元格类型的一些属性(CELL类型)FitText 属性
请参阅 应用于 示例 特性
如果该属性值为 True,则 Microsoft Word 会减小键入单元格中的文字的可视大小,使其适应列宽。Boolean 类型,可读写。说明
如果将 FitText 设置为 True,则虽然字体尺寸没有改变,但将调整字符的可视宽度,使单元格可容纳所有键入的文字。示例
本示例实现的功能是:设定所选内容的第一单元格在其宽度内自动调整键入的文字大小。Selection.Cells(1).FitText = TruePreferredWidth 属性
请参阅 应用于 示例 特性
应用于 Cell、Cells、Column、Columns 和 Table 对象的 PreferredWidth 属性。返回或设置指定的单元格、列或表格的首选宽度(以磅为单位或表示为窗口宽度的百分比)。Single 类型,可读写。expression.PreferredWidthexpression 必需。该表达式返回以上一个对象。WordWrap 属性
请参阅 应用于 示例 特性
应用于 Cell 对象的 WordWrap 属性。如果该属性值为 True,则 Microsoft Word 使文本自动换行并加长单元格,使单元格的宽度保持相同。Boolean 类型,可读写。expression.WordWrapexpression 必需。该表达式返回一个 Cell 对象。