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