VBAでWordドキュメントに行番号を振る
VBAでWordドキュメントに行番号を振る
最近、〈脱・パワポ運動〉の一環として、説明用資料の類をWordで作成するようにしています。
パワポで作るいわゆる「ポンチ絵」のわかりにくさ/非効率を解消するのが目的です。
Wordで作成したドキュメントの「参照指示性」を劇的に上げるための方法が、『シラバス論』の著者、芦田宏直氏が発明した、〈Wordで作成したドキュメント全体に通しで行番号を振る〉というものです。
これ、アホみたいに簡単なんですけど、効果は絶大!
お試しあれ。
文書全体に通しで行番号を振る
これは、めっちゃ簡単。
こういう、ごく普通の文書があるとする。
「ページ レイアウト」タブから
「行番号」を選択し、
「連続番号」を選択すると、
ほれ、このとおり、通しの行番号が振られる。
めちゃくちゃ簡単。たったこれだけのことで、資料の「参照指示性」は飛躍的に高まる。「(○ページの)〇〇行目のところを話しますね。」で済む。
パワポのポンチ絵だとこうはいかない。「えっと、○ページの左上のコマの真ん中やや右のあたりに……」みたいになってわけがわからなくなる。
LineNumberingオブジェクト
この行番号機能を司るのは、VBAの場合、「LineNumbering
オブジェクト」というらしい。
「Word2013 developer docs」(Word2013のオフラインヘルプ)によると、
LineNumbering Object (Word)
Represents line numbers in the left margin or to the left of each newspaper-style column.
Remarks
Use the LineNumbering property to return the LineNumbering object. The following example applies line numbering to the text in the first section of the active document.
VBA With ActiveDocument.Sections(1).PageSetup.LineNumbering .Active = True .CountBy = 5 .RestartMode = wdRestartPage End WithThe following example applies line numbering to the pages in the current section.
VBA Selection.PageSetup.LineNumbering.Active = True
ということらしい。
どうやら、[Document].[Section].PageSetup
オブジェクトのLineNumbering
を参照したら得られるLineNumbering
オブジェクトが司っているものらしい。
LineNumbering
オブジェクトには、八つのプロパティがある。
同じく「Word2013 developer docs」によると、
Name Description Active True if line numbering is active for the specified document, section, or sections. Read/write Long. Application Returns an Application object that represents the Microsoft Word application. CountBy Returns or sets the numeric increment for line numbers. Read/write Long. Creator Returns a 32-bit integer that indicates the application in which the specified object was created. Read-only Long. DistanceFromText Returns or sets the distance (in points) between the right edge of line numbers and the left edge of the document text. Read/write Single. Parent Returns an Object that represents the parent object of the specified LineNumbering object. RestartMode Returns or sets the way line numbering runs -- that is, whether it starts over at the beginning of a new page or section or runs continuously. Read/write WdNumberingRule. StartingNumber Returns or sets the starting line number. Read/write Long.
となっている。
総行番号にする場合だと、
StartingNumber
プロパティを1
に、CountBy
プロパティを1
に、RestartMode
プロパティをwdRestartContinuous
に、Active
プロパティをTrue
に
したらよさげ。
ドキュメントに総行番号を振るコード
ThisDocument
に総行番号を振るだけのコードを示す。
リスト1 標準モジュール
Private Sub activateLineNumbering() Dim lnNumbering As LineNumbering '……(*)' Set lnNumbering = ThisDocument.Sections(1).PageSetup.LineNumbering With lnNumbering .StartingNumber = 1 .CountBy = 1 .RestartMode = wdRestartContinuous .Active = True End With End Sub
通常、(*)のところは、オフラインヘルプのサンプルコードのように
With ThisDocument.Sections(1).PageSetup.LineNumbering
とでも書くのだろうけれど、「LineNumbering
オブジェクトを使っているんだ!」という意識を高めるために(笑)、あえて変数に突っ込んで使っている。
ちなみに、Active
プロパティがなんでBoolean
型でなくてLong
型なのかはわからん。
実行
リスト1を実行してみる。
この状態で実行すると、
バッチリ。
おわりに
これで、たくさんのWordドキュメントに一気に行番号表示させることができる。