Quantcast
Channel: 初心者備忘録
Viewing all articles
Browse latest Browse all 1205

文字列を横方向に移動するWordマクロ(WordBasic編)

$
0
0

いつもお世話になっているWord MVPの新田さんが、まるでカニの動きのように選択文字列を横方向に移動させるWordマクロを紹介されていました。


・【Wordマクロ】文字列を横方向に移動する
http://ameblo.jp/gidgeerock/entry-11609960073.html


面白いマクロだったので、早速私もアイデアをパクッて同様のマクロを考えてみました。

Option Explicit

Public Sub MoveTextRight()
'選択範囲を右に1文字分移動
  Dim strlen As Long
  
  strlen = Selection.End - Selection.Start
  If strlen < 1 Then Exit Sub
  With Application.WordBasic
    .MoveText
    .CharRight 2
    .OK
  End With
  Selection.MoveLeft unit:=wdCharacter, Count:=strlen, Extend:=wdExtend
End Sub

Public Sub MoveTextLeft()
'選択範囲を左に1文字分移動
  Dim strlen As Long
  
  strlen = Selection.End - Selection.Start
  If strlen < 1 Then Exit Sub
  With Application.WordBasic
    .MoveText
    .CharLeft 2
    .OK
  End With
  Selection.MoveRight unit:=wdCharacter, Count:=-1 * strlen, Extend:=wdExtend
End Sub

WordBasicマクロにあるMoveTextステートメントを利用したマクロで、F2キーを押して文字列を移動させる処理(Microsoft Word のキーボード ショートカット 参照)をマクロ化したものです。

実務で使えるかどうかは分かりませんが、動きとしてはとても面白いと思います。

Viewing all articles
Browse latest Browse all 1205

Trending Articles