Delphi7。如何向WORD输出指定字体以及尺寸的文本。分三段,每段字体大小以及居中都不同。(100分)

  • 主题发起人 主题发起人 redleilei
  • 开始时间 开始时间
R

redleilei

Unregistered / Unconfirmed
GUEST, unregistred user!
刚才查看了全文检索,都是胡扯的,没有一个有源码或者能用的。

要求很简单,C:/Program Files/Borland/Delphi7/Demos/ActiveX/OleAuto/Word8这个例子已经实现了大部分我需要的功能。现在唯一要做的就是向WORD输出指定字体以及尺寸的文本。

例如:
第一段输出宋体,四号字,"标题" (居中)
第二段输出黑体,小五,“这是内容”(局左)
第三段输出黑体,五号,“时间“(居右)

谁能给出完整源码,非常感谢。 另外对Excel的操作,各位附上Demo的,发到我邮箱,redleilei@sina.com。另外再送上150分。
下面是Delphi自带Demo的源码,我打算用这个就行了,大侠最好在这个基础上改。
-------
procedure TForm1.LaunchButtonClick(Sender: TObject);
begin
if not Assigned(WordObject) then begin
WordObject := TWordObject.Create;
with WordObject do begin
Caption := 'Delphi is RAD!';
Visible := True;
OnQuit := AppQuit;
OnDocumentChange := AppDocumentChange;
OnNewDocument := DocNew;
OnOpenDocument := DocOpen;
OnCloseDocument := DocClose;
end;
LaunchButton.Enabled := False;
CloseButton.Enabled := True;
NewDocButton.Enabled := True;
end;
end;

procedure TForm1.CloseButtonClick(Sender: TObject);
begin
if Assigned(WordObject) then begin
LaunchButton.Enabled := True;
CloseButton.Enabled := False;
NewDocButton.Enabled := False;
TypeTextButton.Enabled := False;
WordObject.Free;
WordObject := nil;
end;
end;

procedure TForm1.AppDocumentChange(Sender: TObject);
begin
Memo1.Lines.Add('Document changed');
TypeTextButton.Enabled := WordObject.Application.Documents.Count > 0;
end;

procedure TForm1.AppQuit(Sender: TObject);
begin
Memo1.Lines.Add('Quitting Word');
WordObject.Free;
WordObject := nil;
LaunchButton.Enabled := True;
CloseButton.Enabled := False;
NewDocButton.Enabled := False;
TypeTextButton.Enabled := False;
end;

procedure TForm1.DocNew(Sender: TObject);
begin
Memo1.Lines.Add('New document');
end;

procedure TForm1.DocOpen(Sender: TObject);
begin
Memo1.Lines.Add('Document opened');
end;

procedure TForm1.DocClose(Sender: TObject);
begin
Memo1.Lines.Add('Document closed');
end;

procedure TForm1.NewDocButtonClick(Sender: TObject);
begin
try
WordObject.NewDoc('');
except
ShowMessage('It seems like somebody killed Word and didn''t tell me about it...');
if Assigned(WordObject) then begin
LaunchButton.Enabled := True;
CloseButton.Enabled := False;
NewDocButton.Enabled := False;
TypeTextButton.Enabled := False;
WordObject.Free;
WordObject := nil;
end;
end;
end;

procedure TForm1.TypeTextButtonClick(Sender: TObject);
begin
WordObject.InsertText(WordText.Text);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if Assigned(WordObject) then begin
WordObject.Free;
WordObject := nil;
end;
end;
 
郁闷,现在有点技术的都没人懂了
 
郁闷,现在有点技术的都没人懂了
 
wordapplication1.Selection.typetext('标题'+char(#13)));
wordapplication1.Selection.WholeStory;

wordapplication1.Selection.ParagraphFormat.Alignment:= wdAlignParagraphCenter;
with wordapplication1.Selection.Font do begin
NameFarEast:= '宋体';
Size:= 15;
Bold:=1;
end;

filename1:=wdStory;
wordapplication1.Selection.EndKey(filename1,EmptyParam);
wordapplication1.Selection.ParagraphFormat.Alignment:= wdAlignParagraphLeft;
with wordapplication1.Selection.Font do begin
Bold:=0;
Size:= 9;
NameFarEast:= '黑体';
end;
wordapplication1.Selection.typetext(' 这是内容 '+char(#13));

filename1:=wdStory;
wordapplication1.Selection.EndKey(filename1,EmptyParam);
wordapplication1.Selection.ParagraphFormat.Alignment:= wdAlignParagraphright;
with wordapplication1.Selection.Font do begin
Bold:=0;
Size:= 11;
NameFarEast:= '黑体';
end;
wordapplication1.Selection.typetext('时间 '+char(#13));
 
procedure SetQuestionFromDoc(AWordDoc: OleVariant; AFileName: OleVariant);
begin
AWordDoc.Range.InsertAfter(#13);
AWordDoc.Sentences.Last.InsertFile(AFileName, EmptyParam,
EmptyParam, EmptyParam, EmptyParam);
AWordDoc.Range.InsertAfter(#13);
end;

procedure SetTestPaperTitle(AWordDoc: OleVariant; ATitle: string);
begin
AWordDoc.Range.InsertAfter(ATitle + #13);
AWordDoc.Paragraphs.Item(1).Range.Select;
SetFont(AWordDoc, '黑体', wdAlignParagraphCenter, 1, 0, 0, 22);
AWordDoc.Range.InsertAfter(#13 + sTestPaperTitle + #13#13);
AWordDoc.Paragraphs.Item(3).Range.Select;
SetFont(AWordDoc, '宋体', wdAlignParagraphCenter, 0, 0, 0, 8);
end;

procedure SetQuestionTitle(AWordDoc: OleVariant; AQuestionTitle: string);
begin
AWordDoc.Range.InsertAfter(AQuestionTitle + #13);
AWordDoc.Paragraphs.Item(AWordDoc.Paragraphs.Count - 1).Range.Select;
SetFont(AWordDoc, '黑体', wdAlignParagraphLeft, 0, 0, 0, 12);
end;

procedure SetQuestion(AWordDoc: OleVariant; AQuestion: string);
begin
AWordDoc.Range.InsertAfter(AQuestion + #13);
AWordDoc.Paragraphs.Item(AWordDoc.Paragraphs.Count - 1).Range.Select;
SetFont(AWordDoc, '宋体', wdAlignParagraphLeft, 0, 0, 0, 10);
end;

procedure SetFont(AWordDoc: OleVariant; AFontName: string; AFontAlign, ABold, AItalic, AShadow, ASize: Integer);
begin
AWordDoc.Application.Selection.ParagraphFormat.Alignment := AFontAlign;
AWordDoc.Application.Selection.Font.Name := AFontName;
AWordDoc.Application.Selection.Font.Bold := ABold;
AWordDoc.Application.Selection.Font.Italic := AItalic;
AWordDoc.Application.Selection.Font.Shadow := AShadow;
AWordDoc.Application.Selection.Font.size := ASize;
end;
 
后退
顶部