TWordRange

Methods

Constructor.CreateFromBookMark(WordDoc : TWordDoc; BookmarkName : String);
Constructor.CreateFromSelection(WordDoc : TWordDoc);
Constructor.CreateFromDoc(WordDoc : TWordDoc; iStart : Integer = 1; iEnd : Integer = 1);
Constructor.CreateFromRange(WordDoc : TWordDoc; ComRange : Range);
Destructor.Destroy; override;

These create/destroy new TWordRange objects. They will automatically add themselves to the TWordDoc list of ranges and will remove themselves when you free them.

Collapse(oeDirection : TOleEnum = wdCollapseStart);
EndOf(oeUnit : TOleEnum = wdWord; oeExtend : TOleEnum = wdMove) : Integer;
Expand(oeUnit : TOleEnum = wdWord) : Integer;
Move(oeUnit : TOleEnum = wdCharacter; oeCount : Integer = 1) : Integer;
MoveWhile(Cset : String; Count : Integer = wdForward) : Integer;
MoveUntil(Cset : String; Count : Integer = wdForward) : Integer;
MoveStart(oeUnit : TOleEnum = wdCharacter; oeCount : Integer = 1) : Integer;
MoveStartWhile(Cset : String; Count : Integer = wdForward) : Integer;
MoveStartUntil(Cset : String; Count : Integer = wdForward) : Integer;
MoveEnd(oeUnit : TOleEnum = wdCharacter; oeCount : Integer = 1) : Integer;
MoveEndUntil(Cset : String; Count : Integer = wdForward) : Integer;
MoveEndWhile(Cset : String; Count : Integer = wdForward) : Integer;
SetRange(iStart, iEnd : Integer);
StartOf(oeUnit : TOleEnum = wdWord; oeExtend : TOleEnum = wdMove) : Integer;

Various methods to move/extend the range. They act in the same way as the COM range object methods.

GoTo_(oeWhat, oeWhich : TOleEnum; oeCount: Integer = 1; oeName: String = '') : Range;
GotoBookmark(BookmarkName : string) : range;
GoToNext(oeWhat : TOleEnum) : Range;
GoToPrevious(oeWhat : TOleEnum) : Range;
Next(oeUnit : TOleEnum = wdCharacter; oeCount : Integer = 1) : Range;
Previous(oeUnit : TOleEnum = wdCharacter; oeCount : Integer = 1) : Range;

Various methods to move/extend the range. They act in a similar way to the COM range object methods. However the Word versions return the altered range object and do not change the range object that they are called from. This leads to lots of objects floating around - fine in the world of COM where they are cleared up automatically but in Delphi, objects need to be explicitly cleared up. Instead of returning new TWordRange objects (that you would need to free) these methods will alter their calling object's range as well as returning the COM range object. You will usually throw away the COM range object returned by treating the function as a procedure, but you may find it useful. There is thus some inevitable duplication of methods here with the methods in the previous section.

GetNextRange(oeUnit : TOleEnum = wdCharacter; oeCount : Integer = 1) : Range;
GetPreviousRange(oeUnit : TOleEnum = wdCharacter; oeCount : Integer = 1) : Range;

These methods return a COM range object without altering the calling TWordRange object's range. You could combine these methods with CreateFromRange to get a TWordRange object.

InsertAfter(Text : String);
InsertAutoText;
InsertGivenAutoText (AutoText : String; UseRichText : Boolean = True);
InsertBefore(Text : String);
InsertBreak(oeType : TOleEnum = wdPageBreak);
InsertParagraph;
InsertParagraphAfter;
InsertParagraphBefore;
InsertSymbol(CharacterNumber: Integer; Font: String; Unicode: Boolean = False; oeBias : TOleEnum = wdFontBiasDefault);

Various methods to insert characters, text and symbols into the range. They act in the same way as the COM range object methods.

Select;

Makes the Selection object the same as the range object.

Cut;
Copy;
Paste;

NoOfWords : Integer;

Fairly obvious methods and functions

InsertGivenAutoText (AutoText : String; UseRichText : Boolean = True);

This will get the autotext assigned to the variable AutoText and insert it into the range. If UseRichText is true then it uses the style stored with the autotext rather than the style of the range. Note this will not be a very quick way of inserting lots of autotext objects as it requires some late binding (see earlier info on attached templates). However, autotext is a good way of allowing the inserted text to be modified (by altering the template) without changing your program. NB when creating autotext extries, make sure they are stored in the active template and not in normal.dot (or this function will not see them).

CreateBookMark(Bookmark : String);

Allows you to create a bookmark from a range. If you create a range from a bookmark and then alter its text by using the text property, the bookmark is deleted. You can either re-create it or use a bookmark that is just an insertion point and then use the InsertAfter ('…') method to add text. Note that if you do this, the bookmark will remain just an insertion point rather than the whole range added.

Properties

WordDoc : TWordDoc read FWordDoc;

The parent TWordDoc that owns the WordRange object.

Start : Integer read GetStart write SetStart;
End_ : Integer read GetEnd write SetEnd;

Allows you to alter the start and end characters of the range.

Text : String read GetText write SetText;

Allows you to read/write the actual text of the range. Note that the text read is plain ASCII (i.e., no special formatting is retrieved). Setting the Text will insert characters in the prevailing font & style, etc.

Bold : Boolean read GetBold write SetBold;
Italic : Boolean read GetItalic write SetItalic;
Underline : Boolean read GetUnderline write SetUnderline;
Case_ : TOleEnum read GetCase write SetCase;
Font : _Font read GetFont write SetFont;
Style : String read GetStyle write SetStyle;

Reads/Sets these characteristics for the whole range. Note if some of the text is italic and some is not, the property value will return false.

ItemIndex : Integer read FItemIndex;

Returns the index of the TWordRange object within the TWordDoc list.

property Range : Range;

This returns the actual COM Range object, if required. Can also be set (e.g., from one of the functions that returns a COM range object). For example:

RangeOne := TWordRange.CreateFromSelection(WordDoc);
RangeTwo := TWordRange.CreateFromBookmark(WordDoc, 'Title');
...
RangeOne.Range := RangeTwo.GetNextRange;