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.

  • 相关阅读:
    《Machine Learning in Action》—— 白话贝叶斯,“恰瓜群众”应该恰好瓜还是恰坏瓜
    《Machine Learning in Action》—— 女同学问Taoye,KNN应该怎么玩才能通关
    《Machine Learning in Action》—— Taoye给你讲讲决策树到底是支什么“鬼”
    深度学习炼丹术 —— Taoye不讲码德,又水文了,居然写感知器这么简单的内容
    《Machine Learning in Action》—— 浅谈线性回归的那些事
    《Machine Learning in Action》—— 懂的都懂,不懂的也能懂。非线性支持向量机
    《Machine Learning in Action》—— hao朋友,快来玩啊,决策树呦
    《Machine Learning in Action》—— 剖析支持向量机,优化SMO
    《Machine Learning in Action》—— 剖析支持向量机,单手狂撕线性SVM
    JVM 字节码指令
  • 原文地址:https://www.cnblogs.com/zhangzhifeng/p/5249609.html
Copyright © 2011-2022 走看看