Advertisement
gn4711

Word Create Table with Word Document Comments

Apr 29th, 2015
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Explicit
  2.  
  3. Public Sub ExtractCommentsToNewDoc()
  4.  
  5.     '=========================
  6.    'Macro created 2007 by Lene Fredborg, DocTools - www.thedoctools.com
  7.    'Revised October 2013 by Lene Fredborg: Date column added to extract
  8.    'THIS MACRO IS COPYRIGHT. YOU ARE WELCOME TO USE THE MACRO BUT YOU MUST KEEP THE LINE ABOVE.
  9.    'YOU ARE NOT ALLOWED TO PUBLISH THE MACRO AS YOUR OWN, IN WHOLE OR IN PART.
  10.    '=========================
  11.    'The macro creates a new document
  12.    'and extracts all comments from the active document
  13.    'incl. metadata
  14.    
  15.     'Minor adjustments are made to the styles used
  16.    'You may need to change the style settings and table layout to fit your needs
  17.    '=========================
  18.  
  19.     Dim oDoc As Document
  20.     Dim oNewDoc As Document
  21.     Dim oTable As Table
  22.     Dim nCount As Long
  23.     Dim n As Long
  24.     Dim Title As String
  25.    
  26.     Title = "Extract All Comments to New Document"
  27.     Set oDoc = ActiveDocument
  28.     nCount = ActiveDocument.Comments.Count
  29.    
  30.     If nCount = 0 Then
  31.         MsgBox "The active document contains no comments.", vbOKOnly, Title
  32.         GoTo ExitHere
  33.     Else
  34.         'Stop if user does not click Yes
  35.        If MsgBox("Do  you want to extract all comments to a new document?", _
  36.                 vbYesNo + vbQuestion, Title) <> vbYes Then
  37.             GoTo ExitHere
  38.         End If
  39.     End If
  40.        
  41.     Application.ScreenUpdating = False
  42.     'Create a new document for the comments, base on Normal.dotm
  43.    Set oNewDoc = Documents.Add
  44.     'Set to landscape
  45.    oNewDoc.PageSetup.Orientation = wdOrientLandscape
  46.     'Insert a 5-column table for the comments
  47.    With oNewDoc
  48.         .Content = ""
  49.         Set oTable = .Tables.Add _
  50.             (Range:=Selection.Range, _
  51.             NumRows:=nCount + 1, _
  52.             NumColumns:=5)
  53.     End With
  54.    
  55.     'Insert info in header - change date format as you wish
  56.    oNewDoc.Sections(1).Headers(wdHeaderFooterPrimary).Range.Text = _
  57.         "Comments extracted from: " & oDoc.FullName & vbCr & _
  58.         "Created by: " & Application.UserName & vbCr & _
  59.         "Creation date: " & Format(Date, "MMMM d, yyyy")
  60.            
  61.     'Adjust the Normal style and Header style
  62.    With oNewDoc.Styles(wdStyleNormal)
  63.         .Font.Name = "Arial"
  64.         .Font.Size = 10
  65.         .ParagraphFormat.LeftIndent = 0
  66.         .ParagraphFormat.SpaceAfter = 6
  67.     End With
  68.    
  69.     With oNewDoc.Styles(wdStyleHeader)
  70.         .Font.Size = 8
  71.         .ParagraphFormat.SpaceAfter = 0
  72.     End With
  73.  
  74.     'Format the table appropriately
  75.    With oTable
  76.         .Range.Style = wdStyleNormal
  77.         .AllowAutoFit = False
  78.         .PreferredWidthType = wdPreferredWidthPercent
  79.         .PreferredWidth = 100
  80.         .Columns.PreferredWidthType = wdPreferredWidthPercent
  81.         .Columns(1).PreferredWidth = 5
  82.         .Columns(2).PreferredWidth = 23
  83.         .Columns(3).PreferredWidth = 42
  84.         .Columns(4).PreferredWidth = 18
  85.         .Columns(5).PreferredWidth = 12
  86.         .Rows(1).HeadingFormat = True
  87.     End With
  88.  
  89.     'Insert table headings
  90.    With oTable.Rows(1)
  91.         .Range.Font.Bold = True
  92.         .Cells(1).Range.Text = "Page"
  93.         .Cells(2).Range.Text = "Comment scope"
  94.         .Cells(3).Range.Text = "Comment text"
  95.         .Cells(4).Range.Text = "Author"
  96.         .Cells(5).Range.Text = "Date"
  97.     End With
  98.    
  99.     'Get info from each comment from oDoc and insert in table
  100.    For n = 1 To nCount
  101.         With oTable.Rows(n + 1)
  102.             'Page number
  103.            .Cells(1).Range.Text = _
  104.                 oDoc.Comments(n).Scope.Information(wdActiveEndPageNumber)
  105.             'The text marked by the comment
  106.            .Cells(2).Range.Text = oDoc.Comments(n).Scope
  107.             'The comment itself
  108.            .Cells(3).Range.Text = oDoc.Comments(n).Range.Text
  109.             'The comment author
  110.            .Cells(4).Range.Text = oDoc.Comments(n).Author
  111.             'The comment date in format dd-MMM-yyyy
  112.            .Cells(5).Range.Text = Format(oDoc.Comments(n).Date, "dd-MMM-yyyy")
  113.         End With
  114.     Next n
  115.    
  116.     Application.ScreenUpdating = True
  117.     Application.ScreenRefresh
  118.        
  119.     oNewDoc.Activate
  120.     MsgBox nCount & " comments found. Finished creating comments document.", vbOKOnly, Title
  121.  
  122. ExitHere:
  123.     Set oDoc = Nothing
  124.     Set oNewDoc = Nothing
  125.     Set oTable = Nothing
  126.    
  127. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement