VBAでWordドキュメントに行番号を振る

VBAでWordドキュメントに行番号を振る

最近、〈脱・パワポ運動〉の一環として、説明用資料の類をWordで作成するようにしています。

パワポで作るいわゆる「ポンチ絵」のわかりにくさ/非効率を解消するのが目的です。

Wordで作成したドキュメントの「参照指示性」を劇的に上げるための方法が、『シラバス論』の著者、芦田宏直氏が発明した、〈Wordで作成したドキュメント全体に通しで行番号を振る〉というものです。

これ、アホみたいに簡単なんですけど、効果は絶大!

お試しあれ。

文書全体に通しで行番号を振る

これは、めっちゃ簡単。

f:id:akashi_keirin:20200918082851j:plain

こういう、ごく普通の文書があるとする。

f:id:akashi_keirin:20200918082856j:plain

「ページ レイアウト」タブから

f:id:akashi_keirin:20200918082859j:plain

「行番号」を選択し、

f:id:akashi_keirin:20200918082903j:plain

「連続番号」を選択すると、

f:id:akashi_keirin:20200918082909j:plain

ほれ、このとおり、通しの行番号が振られる。

めちゃくちゃ簡単。たったこれだけのことで、資料の「参照指示性」は飛躍的に高まる。「(○ページの)〇〇行目のところを話しますね。」で済む。

パワポポンチ絵だとこうはいかない。「えっと、○ページの左上のコマの真ん中やや右のあたりに……」みたいになってわけがわからなくなる。

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 With

The 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を実行してみる。

f:id:akashi_keirin:20200918082915j:plain

この状態で実行すると、

f:id:akashi_keirin:20200918082919j:plain

f:id:akashi_keirin:20200918082925j:plain

バッチリ。

おわりに

これで、たくさんのWordドキュメントに一気に行番号表示させることができる。