1.WORD OLE 服务器的连接:
app_Word:TwordApplication;
app_Word := TwordApplication.Create(self);
app_Word.Connect;
app_Word.Visible := true;
2. WORD 文档的连结:
doc_Word:TWordDocument;
doc_Word:= TWordDocument.Create(self);
app_Word.Documents.Add(EmptyParam,EmptyParam);
doc_Word.Connect;
3.WORD 字体的设置:
app_Word.Selection.Font.Name :='黑体';
app_Word.Selection.Font.Size := 16;
4.WORD 对齐方式的设置:
app_Word.Selection. ParagraphFormat. Alignment := wdAlignParagraphCenter;
5.WORD 中输出文字:
app_Word.Selection.TypeText(FPageHeader.Caption);
6.WORD 中的换行:
app_Word.Selection. TypeParagraph;
7.WORD 中的表格的建立:
tbl : Table;
tbl := app_Word. ActiveDocument. Tables.Add(app_Word.Selection.Range,RowCount,ColCount);
8.WORD 中光标的定位:
app_Word.Selection.GoTo_(Var What,Which,Count,Name: OleVariant);
What 取值: (wdGoToBookmark,wdGoToSection,wdGoToPage,wdGoToTable,wdGoToLine,wdGoToFootnote,wdGoToEndnote,wdGoToComment,wdGoToField,wdGoToGraphic,wdGoToObject,wdGoToEquation,wdGoToHeading,wdGoToPercent,wdGoToSpellingError,wdGoToGrammaticalError,wdGoToProofreadingError)
Which 取值: (wdGoToFirst,wdGoToLast,,wdGoToNext,wdGoToRelative,wdGoToPrevious,wdGoToAbsolute)
Count 取值:(1,2,3….)
Name 取值:string;一般可以为’’;
9.WORD 中光标的移动:
app_Word.Selection.MoveRight(var Unit,Count,Extend: OleVariant);
Unit 取值: (wdCharacter,wdWord,wdSentence,wdParagraph,wdLine,wdStory,wdScreen,wdSection,wdColumn,wdRow,wdWindow,wdCell,wdCharacterFormatting,wdParagraphFormatting,wdTable,wdItem)
Count 取值:(1,2,3…)
Extend 取值:(wdMove,wdExtend)
编程实例:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,ComObj, Grids, OleServer, Word97;
type
TForm1 = class(TForm)
Button1: TButton;
grd_record: TStringGrid;
app_Word: TWordApplication;
doc_Word: TWordDocument;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var
tbl : Table;
i,j:integer;
un_Var,ex_Var,cnt_Var:OleVariant;
begin
// 在Word中新建一个文档,并添加文本,然后设置粗体和字体大小
app_Word.Connect;
app_Word.Visible := True;
app_Word.Documents.Add(EmptyParam,EmptyParam);
doc_Word.Connect;
app_word.ActiveWindow.View.Type_:= wdNormalView;
app_Word.Selection.Font.Name :='黑体';
app_Word.Selection.Font.Size := 16;
app_Word.Selection.ParagraphFormat.Alignment := wdAlignParagraphCenter;
app_Word.Selection.TypeText('常州市重点外商投资企业在建项目跟踪');
app_Word.Selection.TypeParagraph;
app_Word.Selection.TypeParagraph;
app_Word.Selection.Font.Name := '宋体';
app_Word.Selection.Font.Size :=12;
app_Word.Selection.ParagraphFormat.Alignment := wdAlignParagraphRight;
app_Word.Selection.TypeText('日期:2000年10月20日');
app_Word.Selection.TypeParagraph;
app_Word.Selection.TypeParagraph;
app_Word.Selection.ParagraphFormat.Alignment := wdAlignParagraphLeft;
tbl := app_Word.ActiveDocument.Tables.Add(app_Word.Selection.Range,grd_Record.RowCount,grd_Record.ColCount);
un_Var:=wdCharacter;
cnt_Var:=1;
ex_Var:=wdMove;
for i := 0 to grd_Record.RowCount-1 do // Iterate
begin
for j := 0 to grd_Record.ColCount-1 do // Iterate
begin
app_Word.Selection.TypeText(grd_Record.Cells[j,i]);
app_Word.Selection.MoveRight(un_Var,cnt_Var,ex_Var);
end; // foror
app_Word.Selection.MoveRight(un_Var,cnt_Var,ex_Var);
end; // foror
app_Word.Selection.TypeText('制表人:');
app_Word.Selection.TypeParagraph;
self.Close;
end;
end.