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

先日のWordマクロセミナーの参加者からご要望があり作ってみました。

その方は出版翻訳者で、お客様とコメントを通じてやりとりをされるとのこと。

そのため、1つの文書に自分のコメントの他、相手のコメントが含まれているのです。l

以前紹介した「【コード】コメントを書き出すWordマクロ(その4) ページ番号・行番号付き」の場合、コメントだけが列挙されてしまうので、誰が書いたコメントなのかわかりにくいのです。

そこで、今回のマクロでは、コメントの作成者名も表示することにしました。

このマクロでできること

コメントが挿入されている文書に対してマクロを実行すると、コメントの一覧表を新規ファイルで作成します。

(マクロ実行対象)

コメント

(作成された一覧表)

コメント

マクロの解説

コメントの作者名を、.Author プロパティで取得します。

マクロ


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

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

 If ActiveDocument.Comments.Count = 0 Then Exit Sub

 If MsgBox("ファイル内のすべてのコメントを書き出しますか?", _
   vbQuestion Or vbYesNo, "実施前の確認") = vbNo Then
  Exit Sub
 End If
 
 'オブジェクト変数の設定
 Set actDoc = ActiveDocument
 Set newDoc = Documents.Add
 Set myTable = newDoc.Tables.Add(Range:=Selection.Range, _
       NumRows:=actDoc.Comments.Count + 1, NumColumns:=5)

 '表の項目を追記
 With myTable
  .Cell(1, 1).Range.Text = "P."
  .Cell(1, 2).Range.Text = "行"
  .Cell(1, 3).Range.Text = "対象部分"
  .Cell(1, 4).Range.Text = "作成者"
  .Cell(1, 5).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 = .Author
   myTable.Cell(i + 1, 5).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

コメントに関するマクロ

トップへ戻る