unit ControlWordS;

interface

uses Classes, Sysutils, Word97;

type
  TControlWord = class(TComponent)
  private
    { Private declarations }
    FWordApp : TWordApplication;
  public
    { Public declarations }
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;

function OpenWordFile(DocPath : String) : Boolean;

procedure myAppQuit(Sender: TObject);
    { 將游標移到本行第一碼 }
    procedure MoveToLineFirst;
    { 將游標移到本行最後一碼 }
    procedure MoveToLineEnd(Selected : Boolean);
    { 將游標移到本頁最前 }
    procedure MoveToPageFirst;
    { 將游標移到本頁最後 }
    procedure MoveToPageEnd;
    { 將游標向右移動N碼 }
    procedure MoveToRight(Selected : Boolean; lCount : Integer);
    { 設定書籤 }
    procedure AddBookMark(BookMarkName : String);
    { 移動到指定的書籤上 }
    function GotoBookMark(BookMarkName : String) : Boolean;
    { 切換頁首頁尾 }
    procedure ChangSeekType(ViewType : String);
    { 取得游標現在所在頁次 }
    function GetNowPageNumber : Integer;
    { 存檔 }
    procedure SaveDocument(DocPath : String);

function FindText(KeyStr : String) : Boolean;

published
    { Published declarations }
  end;

implementation

{ TControlWord }

procedure TControlWord.AddBookMark(BookMarkName: String);
var aRange, aDefaultSorting : OleVariant;
begin
  With FWordApp Do
  Begin
    aRange := Selection.Range;
    ActiveDocument.Bookmarks.Add(BookMarkName, aRange);
    aDefaultSorting := wdSortByName;
    ActiveDocument.Bookmarks.DefaultSorting := aDefaultSorting;
    ActiveDocument.Bookmarks.ShowHidden := True;
  End;
end;

procedure TControlWord.ChangSeekType(ViewType: String);
var aSeekTYpe : OleVariant;
begin
  If UpperCase(ViewType) = 'PAGEFOOTER' Then
    aSeekTYpe := wdSeekCurrentPageFooter
  Else If UpperCase(ViewType) = 'PAGEHEADER' Then
    aSeekTYpe := wdSeekCurrentPageHeader
  Else aSeekTYpe := wdSeekMainDocument;
  With FWordApp Do
  Begin
    ActiveWindow.ActivePane.View.SeekView := aSeekTYpe;
  End;
end;

constructor TControlWord.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FWordApp := TWordApplication.Create(Self);
  FWordApp.OnQuit := myAppQuit;
end;

destructor TControlWord.Destroy;
begin
  FWordApp.Disconnect;
  FWordApp.Free;
  inherited Destroy;
end;

function TControlWord.FindText(KeyStr: String): Boolean;
begin
  //
end;

function TControlWord.GetNowPageNumber: Integer;
var
  aPageType : OleVariant;
  NowPageNumber : Integer;
begin
  aPageType := wdActiveEndPageNumber;
  NowPageNumber := FWordApp.Selection.Information[aPageType];
  Result := NowPageNumber;
end;

function TControlWord.GotoBookMark(BookMarkName: String): Boolean;
var aWhat, aWhich, aCount, aName : OleVariant;
begin
  with FWordApp Do
  Begin
    aWhat := wdGoToBookmark;
    aName := BookMarkName;
    Result := True;
    If ActiveDocument.Bookmarks.Exists(aName) Then
      Selection.GoTo_(aWhat, aWhich, aCount, aName)
    Else Result := False;
  End;
end;

procedure TControlWord.MoveToLineEnd(Selected: Boolean);
var aUnit, aExtend : OleVariant;
begin
  With FWordApp Do
  Begin
    aUnit := wdLine;
    aExtend := wdExtend;
    If Selected Then
      Selection.EndKey(aUnit, aExtend)
    Else
      Selection.EndKey(aUnit, EmptyParam);
  End;
end;

procedure TControlWord.MoveToLineFirst;
var aUnit : OleVariant;
begin
  With FWordApp Do
  Begin
    aUnit := wdLine;
    Selection.HomeKey(aUnit, EmptyParam);
  End;
end;

procedure TControlWord.MoveToPageEnd;
var aUnit : OleVariant;
begin
  With FWordApp Do
  Begin
    aUnit := wdStory;
    Selection.EndKey(aUnit, EmptyParam);
  End;
end;

procedure TControlWord.MoveToPageFirst;
var aUnit : OleVariant;
begin
  With FWordApp Do
  Begin
    aUnit := wdStory;
    Selection.HomeKey(aUnit, EmptyParam);
  End;
end;

procedure TControlWord.MoveToRight(Selected: Boolean; lCount: Integer);
var
  aUnit, aExtend, aCount : OleVariant;
begin
  With FWordApp Do
  Begin
    aUnit := wdCharacter;
    aExtend := wdExtend;
    aCount := lCount;
    If Selected Then
      Selection.MoveRight(aUnit, aCount, aExtend)
    Else
      Selection.MoveRight(aUnit, aCount, EmptyParam);    
  End;
end;

procedure TControlWord.myAppQuit(Sender: TObject);
begin
  FWordApp.Disconnect;
end;

function TControlWord.OpenWordFile(DocPath : String): Boolean;
var FFIleName : OleVariant;
begin
  FFileName := DocPath;
  FWordApp.Documents.Open(FFileName, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam);
  FWordApp.Visible := True;
end;

procedure TControlWord.SaveDocument(DocPath: String);
var
  aDocFileName , aDocFileFormat: OleVariant;
begin
  aDocFileName := DocPath;
  aDocFileFormat := wdFormatDocument;
  FWordApp.ActiveDocument.SaveAs(aDocFileName, aDocFileFormat, EmptyParam, EmptyParam,
    EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam);
end;

end.

最新文章

  1. EF Code First 主键对应多个外键
  2. nvl函数 oracle
  3. Windows 下的.NET+ Memcached安装
  4. java 中集合和数组互相转换
  5. jquery 放大图片
  6. BetWeen和模糊查询
  7. [Cocos2d-x]节点的尺寸大小
  8. How to use USB 3G dongle/stick Huawei E169/E620/E800 ( Chip used Qualcomm e1750) in Linux (China and world)
  9. Mysql 5.6 解压版配置方案
  10. [原创]在Centos7.2上源码安装PHP、Nginx、Zentao禅道
  11. 【翻译】使用Ext JS设计响应式应用程序
  12. java 简单程序
  13. 关于 java.toString() ,(String),String.valueOf的区别
  14. Spring Boot Runner启动器
  15. 前端框架VUE----面向对象
  16. vue初始化数据加载
  17. linux文件系统 - 初始化(二)
  18. Extjs gridpanel 合并单元格
  19. 使用Pylint规范你的Python代码
  20. AJAX乱码解决新方法

热门文章

  1. Android 手机震动 设置震动时间
  2. don't panic !
  3. JS创建对象
  4. Java学习笔记(一) java介绍
  5. lintcode 中等题:Intersection of Two Linked Lists 两个链表的交叉
  6. GridView 和ListView中自适应高度
  7. nodejs搭配phantomjs highcharts后台生成图表
  8. KMP入门题目[不定期更新]
  9. 配置centos 7 mysql
  10. OutputStream窥探