From: http://hk.javashuo.com/article/p-uaqvpvre-ge.html
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
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. |
