zoukankan      html  css  js  c++  java
  • Delphi 操作Word怎么控制光标的位置

    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.

  • 相关阅读:
    VIJOS-P1340 拯救ice-cream(广搜+优先级队列)
    uva 11754 Code Feat
    uva11426 GCD Extreme(II)
    uvalive 4119 Always an Interger
    POJ 1442 Black Box 优先队列
    2014上海网络赛 HDU 5053 the Sum of Cube
    uvalive 4795 Paperweight
    uvalive 4589 Asteroids
    uvalive 4973 Ardenia
    DP——数字游戏
  • 原文地址:https://www.cnblogs.com/zhangzhifeng/p/5249609.html
Copyright © 2011-2022 走看看