View Single Post
 
Old 05-14-2021, 06:44 PM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,164
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Firstly, the formatting of text pasted into another document often adapts its look because the style definitions in both docs differs. You can avoid that by creating the new document using the current document. Next you should use Range.FormattedText instead of Range.Text. Then, if you aren't pasting paragraph marks then you probably need to apply the same paragraph style.

When it comes to local formatting, the two red lines 'should' have worked on my machine but they didn't - perhaps you will have more luck with it.
Code:
Sub AnalyseComments()
  Dim oDoc As Document, oNewDoc As Document, oTable As Table
  Dim aCom As Comment, nCount As Long, n As Long
  
  Set oDoc = ActiveDocument
  nCount = ActiveDocument.Comments.Count
  
  'Create a new document for the comments
  Set oNewDoc = Documents.Add(Template:=ActiveDocument.FullName)
  oNewDoc.Range.Delete
  
  'Insert a 4-column table for the comments
  With oNewDoc
    .Content = ""
    Set oTable = .Tables.Add(Range:=Selection.Range, numrows:=nCount + 1, NumColumns:=4)
  End With
  
  With oTable.Rows(1)
    .Range.Font.Bold = True
    .Cells(1).Range.Text = "Page"
    .Cells(2).Range.Text = "Comment scope"
    .Cells(3).Range.Text = "Comment text"
    .Cells(4).Range.Text = "Author"
  End With
  
  'Get info from each comment from oDoc and insert in table
  For n = 1 To nCount
    Set aCom = oDoc.Comments(n)
    With oTable.Rows(n + 1)
      .Cells(1).Range.Text = aCom.Scope.Information(wdActiveEndPageNumber)      'Page number
      .Cells(2).Range.Style = aCom.Scope.Paragraphs(1).Style  'apply the same paragraph style
      .Cells(2).Range.FormattedText = aCom.Scope.FormattedText    'The text marked by the comment
      .Cells(2).Range.Font = aCom.Scope.Font    'this fails even when same attributes over whole range
      .Cells(3).Range.Text = aCom.Range.Text    'The comment itself
      .Cells(4).Range.Text = aCom.Author    'The comment author
    End With
  Next n
  
  oNewDoc.Activate
  MsgBox "Finished creating comments document."
  
  Set oDoc = Nothing
  Set oNewDoc = Nothing
  Set oTable = Nothing
  Set aCom = Nothing

End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote