⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 word.pas

📁 Delphi 7组件与分布式应用开发源码,介绍了基础的组件应用实例
💻 PAS
字号:
unit word;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Word2000, OleServer, ComCtrls, Buttons, ExtCtrls;

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    StartWordBtn: TSpeedButton;
    FormatBtn: TSpeedButton;
    PasteBtn: TSpeedButton;
    SaveBtn: TSpeedButton;
    RichEdit1: TRichEdit;
    WordApplication1: TWordApplication;
    Doc: TWordDocument;
    ParaFmt: TWordParagraphFormat;
    WordFont1: TWordFont;
    CloseBtn: TButton;
    procedure CloseBtnClick(Sender: TObject);
    procedure StartWordBtnClick(Sender: TObject);
    procedure PasteBtnClick(Sender: TObject);
    procedure FormatBtnClick(Sender: TObject);
    procedure ReplaceBtnClick(Sender: TObject);
    procedure SaveBtnClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    procedure EnterText;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}



procedure TForm1.CloseBtnClick(Sender: TObject);
var
  SaveChanges : OleVariant;
begin
  { Make sure we aren't prompted to save the document }
  SaveChanges := wdDoNotSaveChanges;
  WordApplication1.Quit(SaveChanges);
  WordApplication1.Disconnect;
  Close;
end;
procedure TForm1.EnterText;
var
  S: WordSelection;//OleVariant;
begin
  S := WordApplication1.Selection;
  WordFont1.ConnectTo(S.Font);


  S.TypeText('程序创建的新文件(页标题)');
  S.TypeParagraph;
  S.TypeText('COM服务器(页的小标题)');
  S.TypeParagraph;

  //加入一个书签,后面粘贴时用到
 Doc.Bookmarks.Add('FirstPara', EmptyParam);
  S.TypeText('内容:这是个示例程序,用于介绍COM服务器组件的使用');
  S.TypeText('DELPHI 7的联机帮助中没有介绍这个组件页中的组件的使用方法');
  S.TypeText('甚至没有基本的');
  WordFont1.Italic := wdToggle;
  S.TypeText('组件说明');
  WordFont1.Italic := wdToggle;
  S.TypeText('希望本书,及本程序对大家有所帮助');
  S.TypeText('更多的资料,可以通过Google在网上搜索');
  S.TypeParagraph;
end;


procedure TForm1.StartWordBtnClick(Sender: TObject);
begin
  { In this example, the server's Autoconnect property is false,
    so we start Word in code }
  WordApplication1.Connect;
  WordApplication1.Visible := True;
  Doc.ConnectTo(WordApplication1.Documents.Add(EmptyParam,EmptyParam,EmptyParam,EmptyParam));
  EnterText;
  FormatBtn.Enabled := True;
  PasteBtn.Enabled := True;

  SaveBtn.Enabled := True;
end;

procedure TForm1.PasteBtnClick(Sender: TObject);
var
  BkMk: Range;
  BMName, Format: OleVariant;
begin
  RichEdit1.SelectAll;
  RichEdit1.CopyToClipboard;
  RichEdit1.SelStart := RichEdit1.SelLength;

  { Paste the text in at the bookmark called 'FirstPara' }
  BMName := 'FirstPara';
  Format := wdPasteRTF;
  BkMk := Doc.Bookmarks.Item(BMName).Range;
  BkMk.PasteSpecial(EmptyParam, EmptyParam, EmptyParam,
                    EmptyParam, Format, EmptyParam, EmptyParam);
end;

procedure TForm1.FormatBtnClick(Sender: TObject);
var
  Body: Range;
  HowFar, Extend: OleVariant;
begin
  WordApplication1.ScreenUpdating := False;
  try
    { First the title }
    ParaFmt.ConnectTo(Doc.Paragraphs.Item(1).Format);
    WordFont1.ConnectTo(Doc.Paragraphs.Item(1).Range.Font);
    ParaFmt.Alignment := wdAlignParagraphCenter;
    ParaFmt.SpaceAfter := 24;
    // The casts to TOleEnum are to eliminate compiler warnings about subrange bounds
    ParaFmt.Borders.Item(LongWord(wdBorderTop)).LineStyle := wdLineStyleSingle;
    ParaFmt.Borders.Item(LongWord(wdBorderBottom)).LineStyle := wdLineStyleDouble;
//    TOleEnum
    WordFont1.Size := 36;
    WordFont1.Name := 'Arial';
    WordFont1.Bold := integer(True);

    { Next the heading }
    ParaFmt.ConnectTo(Doc.Paragraphs.Item(2).Format);
    WordFont1.ConnectTo(Doc.Paragraphs.Item(2).Range.Font);
    ParaFmt.SpaceAfter := 12;
    WordFont1.Size := 16;
    WordFont1.Bold := integer(True);
    WordFont1.Italic := integer(True);

    { Finally the rest of the text - which may be more than one paragraph }
    Body := Doc.Paragraphs.Item(3).Range;
    HowFar := wdStory;
    Extend := wdExtend;
    Body.EndOf(HowFar, Extend);
    ParaFmt.ConnectTo(Body.ParagraphFormat);
    WordFont1.ConnectTo(Body.Font);
    ParaFmt.FirstLineIndent := 24;
    ParaFmt.LineSpacing := WordFont1.Size * 1.2;
  finally
    WordApplication1.ScreenUpdating := True;
  end;
end;

procedure TForm1.ReplaceBtnClick(Sender: TObject);
var
  SearchText, ReplaceText, ReplaceWidth,Wrap, All: OleVariant;
  TextRange: Range;
begin
  { Replace text }
  SearchText := 'pathetic';
  ReplaceText := 'marvellous';
  ReplaceWidth := strlen('marvellous');
  Wrap := wdFindContinue;
  All := wdReplaceAll;
  Doc.Content.Find.Execute(SearchText, EmptyParam, EmptyParam, EmptyParam,
                           EmptyParam, EmptyParam, EmptyParam,Wrap,
                           EmptyParam,EmptyParam, ReplaceText,EmptyParam,EmptyParam,EmptyParam, EmptyParam);

  { Find text and format it. This time we define our own range, so that
    it can be redefined when the Find is executed. }
  TextRange := Doc.Content;
  SearchText := 'lorem ipsum dolor';
  ReplaceWidth := 0;
  TextRange.Find.Execute(SearchText, EmptyParam, EmptyParam, EmptyParam,
                         EmptyParam, EmptyParam, EmptyParam, Wrap,
                         EmptyParam, EmptyParam, EmptyParam,EmptyParam, EmptyParam, EmptyParam,EmptyParam);
  TextRange.Font.Italic := integer(True);
end;

procedure TForm1.SaveBtnClick(Sender: TObject);
begin
//  Hide;
  try
    WordApplication1.Dialogs.Item(wdDialogFileSaveAs).Show(EmptyParam);
  finally
    Show;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  RichEdit1.Lines.LoadFromFile('Sample.doc');
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -