【Word VBA】コメントを書き出すWordマクロ(その4) ページ番号・行番号付き

これまでに、コメントを書き出すマクロコメントを書き出すマクロ(その2)ページ番号とともにWordのコメントを書き出す とご紹介してきました。

少しだけ改造したものをご紹介します。

先日の日本翻訳連盟のセミナー にて紹介したところ、かなり喜んでいただけました。

Wordマクロのうれしさを紹介するにはわかりやすいマクロですよね。

このマクロでできること

文書に記載されたコメントをページ番号と行番号とともに書き出します。

マクロの解説

赤文字の部分があるため、文書中にコメントがない場合には終了します。

.Information(wdFirstCharacterLineNumber)

Informationプロパティにて、コメントがついている対象となる文字列の行番号を取得しています。

マクロ


Sub コメントを別紙に書き出すマクロ_4()

 Dim i As Integer
 Dim actDoc As Document
 Dim newDoc As Document
 Dim myTable As Table

 If ActiveDocument.Comments.Count = 0 Then Exit Sub

 'オブジェクト変数の設定
 Set actDoc = ActiveDocument
 Set newDoc = Documents.Add
 Set myTable = newDoc.Tables.Add(Range:=Selection.Range, _
       NumRows:=actDoc.Comments.Count + 1, NumColumns:=4)

 '表の項目を追記
 With myTable
  .Cell(1, 1).Range.Text = "P."
  .Cell(1, 2).Range.Text = "行"
  .Cell(1, 3).Range.Text = "対象部分"
  .Cell(1, 4).Range.Text = "コメント"
  .Rows(1).Select
  With Selection
   .ParagraphFormat.Alignment = wdAlignParagraphCenter
   .Collapse direction:=wdCollapseStart
  End With
 End With

 'ページ番号とコメントを表に記入
 For i = 1 To actDoc.Comments.Count
  With actDoc.Comments(i)
   myTable.Cell(i + 1, 1).Range.Text = _
    .Scope.Information(wdActiveEndPageNumber)
   myTable.Cell(i + 1, 2).Range.Text = _
    .Scope.Information(wdFirstCharacterLineNumber)
   myTable.Cell(i + 1, 3).Range.Text = .Scope.Text
   myTable.Cell(i + 1, 4).Range.Text = .Range.Text
  End With
 Next i

 '表のスタイルを設定
 With myTable
  .Style = "表 (格子)"
  .AutoFitBehavior (wdAutoFitContent)
 End With

 'オブジェクト変数の解放
 Set actDoc = Nothing
 Set newDoc = Nothing
 Set myTable = Nothing

End Sub

コメントに関するマクロ

トップへ戻る