ドラッグで文字列を選択したかのような操作(Word)

文字列をドラッグして選択する操作(Word)

akashi-keirin.hatenablog.com

このとき、

選択範囲を文字数単位で拡張するメソッドとか、ないものか。(←調べろ。)

などと、テキトーなことをぶっこいていたが、

あった。

やはり、ものごとというものは、ちゃんと調べてみるものである。

Selection.MoveLeftメソッド

ちょこっと調べてみると、Selection.MoveLeftメソッドというものが見つかった。

コチラによると、

Moves the selection to the left and returns the number of units it has been moved.

Syntax

expression.

expression Required. A variable that represents a Selection object.

Parameters

Name Required/Optional Data type Description
Unit Optional WdUnits The unit by which the selection is to be moved.The default value is wdCharacter.
Count Optional Variant The number of units the selection is to be moved. The default value is 1.
Extend Optional Variant Can be either wdMove or wdExtend. If wdMove is used, the selection is collapsed to the endpoint and moved to the left. If wdExtend is used, the selection is extended to the left. The default value is wdMove.

とある。

つまり、たとえば、

Call Selection.MoveLeft(wdCharacter, 5, wdExtend)

と書けば、

現在のカーソル位置から〈文字単位で〉、〈5だけ〉、左方向へ〈選択範囲を延ばす〉という操作を行うことができるということだ。

実験

f:id:akashi_keirin:20190710075155j:plain

このように、「キーワード」と「(●●●●●)」の嵌張にカーソルを置いて、イミディエイト・ウインドウに

Call Selection.MoveLeft(wdCharacter, 5, wdExtend)

と打ち込んで[Enter]を押す。

f:id:akashi_keirin:20190710075159j:plain

この通り。

おわりに

これでかなり処理速度を上げることができるぞ!

うおおおおおおおおおおおお!

面白くなってきたぜええええ!

ちなみに、右方向に選択範囲を延ばすには、Selection.MoveRightメソッドをどうぞ。