Delphi 操作Word怎么控制光标的位置
2016-03-07 10:17
489 查看
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.
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.
相关文章推荐
- delphi ole word
- Delphi Excel FastReport
- Delphi word编辑
- Delphi 操作word 表格
- delphi 设置表格样式。
- delphi通过OLE对word进行单元格合并操作
- Delphi 6常用日期与时间函数
- Delphi常用日期函数
- delphi中Message消息的使用方法
- 现在, Delphi 的多线程已经非常易用了!
- Delphi实现截屏存盘的方法
- Why we need interfaces in Delphi
- TMsgThread, TCommThread -- 在delphi线程中实现消息循环(105篇博客,好多研究消息的文章)
- 深入Delphi -- Windows 消息机制
- Delphi -- 创建 桌面、发送到...、快速启动栏、开始菜单、程序菜单、右键菜 单
- 关于Delphi中的字符串的浅析(瓢虫大作,里面有内存错误的举例)
- String[255]在高版本Delphi里还是被解释成Byte,总体长度256,使用StrPCopy可以给Array String拷贝字符串(内含许多实验测试)
- 外国的Delphi网站
- Delphi word
- delphi 数据导出到word