📄 ahword97.pas
字号:
FItemIndex := FWordDoc.NoOfRanges;
FWordDoc.FRanges.Add (self);
except
FComRange := nil;
raise Exception.CreateFmt ('Bookmark "%s" not found in document "%s"',
[BookMarkName, FWordDoc.FComDoc.Name]);
// NB raising an exception here will cause TWordRange.Destroy to be called
// The exception will be from trying to get an invalid bookmark
// ie before ItemIndex is given a valid number, hence preset to -1
// TWordRange.Destroy will only try to remove the range from the TWordDoc list
// if ItemIndex <> -1
end
end;
constructor TWordRange.CreateFromSelection(WordDoc : TWordDoc);
begin
Create;
FItemIndex := -1;
FWordDoc := WordDoc;
FComRange := FWordDoc.WordApp.Application.Selection.Range;
FItemIndex := FWordDoc.NoOfRanges;
FWordDoc.FRanges.Add (self);
end;
constructor TWordRange.CreateFromDoc(WordDoc : TWordDoc; iStart : Integer = 1; iEnd : Integer = 1);
var
ovStart, ovEnd : OleVariant;
begin
Create;
FItemIndex := -1;
ovStart := iStart;
ovEnd := iEnd;
FWordDoc := WordDoc;
FComRange := FWordDoc.FComDoc.Range (ovStart, ovEnd);
FItemIndex := FWordDoc.NoOfRanges;
FWordDoc.FRanges.Add (self);
end;
constructor TWordRange.CreateFromRange(WordDoc : TWordDoc; ComRange : {$IFDEF VER130}Word97.Range{$ELSE}Word_TLB.Range{$ENDIF});
begin
Create;
FItemIndex := -1;
FWordDoc := WordDoc;
FComRange := ComRange;
FItemIndex := FWordDoc.NoOfRanges;
FWordDoc.FRanges.Add (self);
end;
destructor TWordRange.Destroy;
begin
if FItemIndex <> -1 then FWordDoc.RemoveRange (FItemIndex);
inherited;
end;
function TWordRange.EndOf(oeUnit : TOleEnum = wdWord; oeExtend : TOleEnum = wdMove) : Integer;
var
ovUnit,
ovExtend : OleVariant;
begin
ovUnit := oeUnit;
ovExtend := oeExtend;
EndOf := FComRange.EndOf (ovUnit, ovExtend);
end;
function TWordRange.Expand(oeUnit: TOleEnum = wdWord) : Integer;
var
ovUnit : OleVariant;
begin
ovUnit := oeUnit;
Expand := FComRange.Expand (ovUnit);
end;
function TWordRange.GetBold: Boolean;
begin
GetBold := FComRange.Bold = wdTrue;
end;
function TWordRange.GetCase: TOleEnum;
begin
GetCase := FComRange.Case_
end;
function TWordRange.GetEnd: Integer;
begin
GetEnd := FComRange.End_;
end;
function TWordRange.GetFont: _Font;
begin
GetFont := FComRange.Font;
end;
function TWordRange.GetItalic: Boolean;
begin
GetItalic := FComRange.Italic = wdTrue
end;
function TWordRange.GetStart: Integer;
begin
GetStart := FComRange.Start;
end;
function TWordRange.GetStyle: String;
begin
GetStyle := FComRange.Get_Style;
end;
function TWordRange.GetText: String;
begin
GetText := FComRange.Text;
end;
function TWordRange.GetUnderline: Boolean;
begin
GetUnderline := FComRange.Underline = wdTrue
end;
function TWordRange.GoTo_(oeWhat, oeWhich: TOleEnum; oeCount: Integer = 1; oeName: String = '') : {$IFDEF VER130}Word97.Range{$ELSE}Word_TLB.Range{$ENDIF};
var
ovWhat, ovWhich, ovCount, ovName : OleVariant;
begin
ovWhat := oeWhat;
ovWhich := oeWhich;
ovCount := ovCount;
if ovName = '' then
ovName := EmptyParam
else
ovName := ovName;
FComRange := FComRange.GoTo_ (ovWhat, ovWhich, ovCount, ovName);
Goto_ := FComRange;
end;
function TWordRange.GotoBookmark(BookmarkName : string) : {$IFDEF VER130}Word97.Range{$ELSE}Word_TLB.Range{$ENDIF};
var
What : OLEVariant;
Which : OLEVariant;
Count : OLEVariant;
Name : OLEVariant;
begin
What := wdGoToBookmark;
Which := EmptyParam;
Count := EmptyParam;
Name := BookmarkName;
try
FComRange := FComRange.GoTo_(What, Which, Count, Name);
except
FComRange := nil;
raise Exception.CreateFmt ('Bookmark "%s" not found in document "%s"',
[BookMarkName, FWordDoc.FComDoc.Name]);
end;
GotoBookmark := FComRange;
end;
function TWordRange.GoToNext(oeWhat: TOleEnum) : {$IFDEF VER130}Word97.Range{$ELSE}Word_TLB.Range{$ENDIF};
// moves range to next
var
ovWhat : OleVariant;
begin
ovWhat := oeWhat;
FComRange := FComRange.GoToNext (ovWhat);
GotoNext := FComRange;
end;
function TWordRange.GoToPrevious(oeWhat: TOleEnum) : {$IFDEF VER130}Word97.Range{$ELSE}Word_TLB.Range{$ENDIF};
// moves range to previous
var
ovWhat : OleVariant;
begin
ovWhat := oeWhat;
FComRange := FComRange.GoToPrevious (ovWhat);
GotoPrevious := FComRange;
end;
function TWordRange.NoOfWords : Integer;
begin
NoOfWords := FComRange.ComputeStatistics (wdStatisticWords)
end;
procedure TWordRange.InsertAfter(Text: String);
begin
FComRange.InsertAfter (Text);
end;
procedure TWordRange.InsertAutoText;
begin
FComRange.InsertAutoText
end;
procedure TWordRange.InsertGivenAutoText (AutoText : String; UseRichText : Boolean = True);
var
ATemplate : OleVariant;
AnAutoText : OleVariant;
begin
// NB Word_TLB import seems to fail with Get_AttachedTemplate and returns an OleVariant
// rather than a Template type. Therefor this bit of code is late bound and may not work in
// other languages - sorry
try
ATemplate := FWordDoc.FComDoc.Get_AttachedTemplate;
AnAutoText := ATemplate.AutoTextEntries.Item (AutoText);
AnAutoText.Insert (FComRange, UseRichText);
except
on E : EOleSysError do
if E.ErrorCode = 5941 then
raise Exception.CreateFmt ('Error Autotext "%s" not found in template "%s"',
[AutoText, ATemplate.Name])
else
raise Exception.CreateFmt ('Error inserting Autotext "%s" from template "%s"',
[AutoText, ATemplate.Name])
end;
end;
procedure TWordRange.InsertBefore(Text: String);
begin
FComRange.InsertBefore (Text);
end;
procedure TWordRange.InsertBreak(oeType: TOleEnum = wdPageBreak);
var
ovType : OleVariant;
begin
ovType := oeType;
FComRange.InsertBreak (ovType);
end;
procedure TWordRange.InsertParagraph;
begin
FComRange.InsertParagraph
end;
procedure TWordRange.InsertParagraphAfter;
begin
FComRange.InsertParagraphAfter
end;
procedure TWordRange.InsertParagraphBefore;
begin
FComRange.InsertParagraphBefore
end;
procedure TWordRange.InsertSymbol(CharacterNumber: Integer; Font: String;
Unicode: Boolean = False; oeBias : TOleEnum = wdFontBiasDefault);
var
ovFont, ovUnicode, ovBias : OleVariant;
begin
ovFont := Font;
ovUnicode := UniCode;
ovBias := oeBias;
FComRange.InsertSymbol (CharacterNumber, ovFont, ovUnicode, ovBias);
end;
function TWordRange.Move(oeUnit: TOleEnum = wdCharacter; oeCount: Integer = 1) : Integer;
var
ovUnit,
ovCount : OleVariant;
begin
ovUnit := oeUnit;
ovCount := oeCount;
Move := FComRange.Move (ovUnit, ovCount);
end;
function TWordRange.MoveEnd(oeUnit: TOleEnum = wdCharacter; oeCount: Integer = 1) : Integer;
var
ovUnit,
ovCount : OleVariant;
begin
ovUnit := oeUnit;
ovCount := oeCount;
MoveEnd := FComRange.MoveEnd (ovUnit, ovCount);
end;
function TWordRange.MoveEndUntil(Cset: String; Count: Integer = wdForward) : Integer;
var
ovCset, ovCount : OleVariant;
begin
ovCset := Cset;
ovCount := Count;
MoveEndUntil := FComRange.MoveEndUntil (ovCset, ovCount);
end;
function TWordRange.MoveEndWhile(Cset: String; Count: Integer = wdForward) : Integer;
var
ovCset, ovCount : OleVariant;
begin
ovCset := Cset;
ovCount := Count;
MoveEndWhile := FComRange.MoveEndWhile (ovCset, ovCount);
end;
function TWordRange.MoveStart(oeUnit: TOleEnum = wdCharacter; oeCount: Integer = 1) : Integer;
var
ovUnit,
ovCount : OleVariant;
begin
ovUnit := oeUnit;
ovCount := oeCount;
MoveStart := FComRange.MoveStart (ovUnit, ovCount);
end;
function TWordRange.MoveStartUntil(Cset: String; Count: Integer = wdForward) : Integer;
var
ovCset, ovCount : OleVariant;
begin
ovCset := Cset;
ovCount := Count;
MoveStartUntil := FComRange.MoveStartUntil (ovCset, ovCount);
end;
function TWordRange.MoveStartWhile(Cset: String; Count: Integer = wdForward) : Integer;
var
ovCset, ovCount : OleVariant;
begin
ovCset := Cset;
ovCount := Count;
MoveStartWhile := FComRange.MoveStartWhile (ovCset, ovCount);
end;
function TWordRange.MoveUntil(Cset: String; Count: Integer = wdForward) : Integer;
var
ovCset, ovCount : OleVariant;
begin
ovCset := Cset;
ovCount := Count;
MoveUntil := FComRange.MoveUntil (ovCset, ovCount);
end;
function TWordRange.MoveWhile(Cset: String; Count: Integer = wdForward) : Integer;
var
ovCset, ovCount : OleVariant;
begin
ovCset := Cset;
ovCount := Count;
MoveWhile := FComRange.MoveWhile (ovCset, ovCount);
end;
function TWordRange.Next(oeUnit: TOleEnum = wdCharacter; oeCount: Integer = 1) : {$IFDEF VER130}Word97.Range{$ELSE}Word_TLB.Range{$ENDIF};
// moves range to next
var
ovUnit,
ovCount : OleVariant;
begin
ovUnit := oeUnit;
ovCount := oeCount;
FComRange := FComRange.Next (ovUnit, ovCount);
Next := FComRange;
end;
function TWordRange.Previous(oeUnit: TOleEnum = wdCharacter; oeCount: Integer = 1) : {$IFDEF VER130}Word97.Range{$ELSE}Word_TLB.Range{$ENDIF};
// moves range to previous
var
ovUnit,
ovCount : OleVariant;
begin
ovUnit := oeUnit;
ovCount := oeCount;
FComRange := FComRange.Previous (ovUnit, ovCount);
Previous := FComRange;
end;
function TWordRange.GetNextRange(oeUnit: TOleEnum = wdCharacter; oeCount: Integer = 1) : {$IFDEF VER130}Word97.Range{$ELSE}Word_TLB.Range{$ENDIF};
// returns a range next to this one (does not alter this range)
var
ovUnit,
ovCount : OleVariant;
begin
ovUnit := oeUnit;
ovCount := oeCount;
GetNextRange := FComRange.Next (ovUnit, ovCount);
end;
function TWordRange.GetPreviousRange(oeUnit: TOleEnum = wdCharacter; oeCount: Integer = 1) : {$IFDEF VER130}Word97.Range{$ELSE}Word_TLB.Range{$ENDIF};
// returns a range prior to this one (does not alter this range)
var
ovUnit,
ovCount : OleVariant;
begin
ovUnit := oeUnit;
ovCount := oeCount;
GetPreviousRange := FComRange.Previous (ovUnit, ovCount);
end;
procedure TWordRange.SetBold(Value: Boolean);
begin
if Value then FComRange.Bold := wdTrue else FComRange.Bold := wdFalse;
end;
procedure TWordRange.SetCase(oeValue: TOleEnum);
begin
FComRange.Case_ := oeValue;
end;
procedure TWordRange.SetEnd(Value: Integer);
begin
FComRange.End_ := Value;
end;
procedure TWordRange.SetFont(fFont: _Font);
begin
FComRange.Font := fFont;
end;
procedure TWordRange.SetItalic(Value: Boolean);
begin
if Value then FComRange.Italic := wdTrue else FComRange.Italic := wdFalse;
end;
procedure TWordRange.SetRange(iStart, iEnd: Integer);
begin
FComRange.SetRange (iStart, iEnd);
end;
procedure TWordRange.SetStart(Value: Integer);
begin
FComRange.Start := Value;
end;
procedure TWordRange.SetStyle(Style: String);
var
ovStyle : OleVariant;
begin
ovStyle := Style;
FComRange.Set_Style (ovStyle);
end;
procedure TWordRange.SetText(Value: String);
begin
FComRange.Text := Value;
end;
procedure TWordRange.SetUnderline(Value: Boolean);
begin
if Value then FComRange.Underline := wdTrue else FComRange.Underline := wdFalse;
end;
function TWordRange.StartOf(oeUnit : TOleEnum = wdWord; oeExtend : TOleEnum = wdMove): Integer;
var
ovUnit,
ovExtend : OleVariant;
begin
ovUnit := oeUnit;
ovExtend := oeExtend;
StartOf := FComRange.StartOf (ovUnit, ovExtend);
end;
procedure TWordRange.CreateBookMark(BookmarkName : String);
var
ovRange : OleVariant;
begin
ovRange := FComRange;
FWordDoc.FComDoc.Bookmarks.Add (BookmarkName, ovRange);
end;
procedure TWordRange.Select;
begin
FComRange.Select;
end;
procedure TWordRange.Cut;
begin
FComRange.Cut;
end;
procedure TWordRange.Copy;
begin
FComRange.Copy;
end;
procedure TWordRange.Paste;
begin
FComRange.Paste;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -