先在delphi的severs面板上面选择WordApplication,WordDocument,WordFont组件加入到你的form,引用下面单元
调用CreateWordDocument(),传递WordApplication,WordDocument两个参数就行了,
SetFont(Font, Document, 1, 0, 0, 24);
传递WordFont和WordDocument,后面的参数是粗体,斜体,阴影,和大小,自己看着设置就行了
unit MakePaper;
interface
uses
Windows, Messages, WordXP, OleServer, SysUtils, Forms, Variants, DBTables,
Classes;
//设置Word文档自体,在调用之前要先使用WordFont1.ConnectTo(WordDocument1.Sentences.Last.Font);
//连接到文档
procedure SetFont(var WordFont:TWordFont; var WordDocument:TWordDocument; aBold,
aItalic, aShadow, aSize:Integer);
procedure CreateWordDocument(var WordApplication:TWordApplication; var
WordDocument:TWordDocument);
implementation
procedure SetFont(var WordFont:TWordFont; var WordDocument:TWordDocument; aBold,
aItalic, aShadow, aSize:integer);
begin
WordFont.ConnectTo(WordDocument.Sentences.Get_Last.Font);
WordFont.Name := '宋体';
WordFont.Bold := aBold;
WordFont.Italic := aItalic;
WordFont.Shadow := aShadow;
WordFont.Size := aSize;
end;
procedure CreateWordDocument(var WordApplication:TWordApplication; var
WordDocument:TWordDocument);
var
Docs, Template, NewTemplate, ItemIndex:OleVariant;
begin
try
Template := EmptyParam;
NewTemplate := True;
ItemIndex := 1;
try
WordApplication.Connect;
except
MessageBox(Application.Handle,
'您可能没有安装Word!建议您安装WordXP,并选择完全安装!如果还有问题请检查您的系统!',
'错误', MB_OK + MB_ICONSTOP);
Abort;
end;
WordApplication.Visible := True;
WordApplication.Caption := '电子试卷生成系统';
Template := EmptyParam;
NewTemplate := False;
Docs := WordApplication.Documents;
Docs.Add(NewTemplate := True);
WordDocument.ConnectTo(WordApplication.Documents.Item(ItemIndex));
//关闭语法检查和拼写检查
WordApplication.Options.CheckSpellingAsYouType := False;
WordApplication.Options.CheckGrammarAsYouType := False;
except
on E:Exception do
begin
MessageBox(Application.Handle, PChar(E.Message), '错误', MB_OK +
MB_ICONSTOP);
WordApplication.Disconnect;
end;
end;
end;