Extract Tracked Changes To New Doc
Extract Tracked Changes To New Doc
'=========================
'Macro created 2007 by Lene Fredborg, DocTools - www.thedoctools.com
'THIS MACRO IS COPYRIGHT. YOU ARE WELCOME TO USE THE MACRO BUT YOU MUST KEEP
THE LINE ABOVE.
'YOU ARE NOT ALLOWED TO PUBLISH THE MACRO AS YOUR OWN, IN WHOLE OR IN PART.
'Adopted for use by Desmund Hui, 2018
'=========================
'The macro creates a new document
'and extracts insertions and deletions
'marked as tracked changes from the active document
'The document will also include metadata
'Inserted text will be applied automatic (black) font color
'Deleted text will be applied red font color
If oDoc.Revisions.Count = 0 Then
MsgBox "The active document contains no tracked changes.", vbOKOnly, Title
GoTo ExitHere
Else
'Stop if user does not click Yes
If MsgBox("Do you want to extract tracked changes to a new document?", _
vbYesNo + vbQuestion, Title) <> vbYes Then
GoTo ExitHere
End If
End If
Application.ScreenUpdating = False
'Create a new document for the tracked changes, base on Normal.dot
Set oNewDoc = Documents.Add
'Set to landscape if necessary
'oNewDoc.PageSetup.Orientation = wdOrientLandscape
With oNewDoc
'Make sure any content is deleted
.Content = ""
'Set appropriate margins
With .PageSetup
.PaperSize = wdPaperA4
.LeftMargin = CentimetersToPoints(1)
.RightMargin = CentimetersToPoints(1)
.TopMargin = CentimetersToPoints(1)
End With
'Insert a 5-column table for the tracked changes and metadata
Set oTable = .Tables.Add _
(Range:=Selection.Range, _
numrows:=1, _
NumColumns:=5)
End With
With oNewDoc.Styles(wdStyleHeader)
.Font.Size = 8
.ParagraphFormat.SpaceAfter = 0
End With
'Get info from each tracked change from oDoc and insert in table
For Each oRevision In oDoc.Revisions
Select Case oRevision.Type
'Only include insertions, deletions, and formatting changes
Case wdRevisionInsert, wdRevisionDelete, wdRevisionProperty
'In case of footnote/endnote references (appear as Chr(2)),
'insert "[footnote reference]"/"[endnote reference]"
With oRevision
'Get the changed text
strText = .Range.Text
If oRange.Footnotes.Count = 1 Then
strText = Replace(Expression:=strText, _
Find:=Chr(2), Replace:="[footnote reference]",
_
Start:=1, Count:=1)
'To keep track of replace, adjust oRange to start after
i
oRange.Start = oRange.Start + i
'Type of revision
If oRevision.Type = wdRevisionInsert Then
.Cells(3).Range.Text = "Inserted"
'Apply automatic color (black on white)
oRow.Range.Font.Color = wdColorAutomatic
ElseIf oRevision.Type = wdRevisionDelete Then
.Cells(3).Range.Text = "Deleted"
'Apply red color
oRow.Range.Font.Color = wdColorRed
Else
.Cells(3).Range.Text = "DCD related" 'sth
oRow.Range.Font.Color = wdColorAutomatic
End If
'.Cells(4).Range.Text = _
Sections(oRevision.Range.Information(wdActiveEndSectionNumber)) _
.Range.Paragraphs(1).Range.Text
'Changed text
.Cells(5).Range.Text = strText
Application.ScreenUpdating = True
Application.ScreenRefresh
oNewDoc.Activate
MsgBox n & " tracked changed have been extracted. " & _
"Finished creating document.", vbOKOnly, Title
ExitHere:
Set oDoc = Nothing
Set oNewDoc = Nothing
Set oTable = Nothing
Set oRow = Nothing
Set oRange = Nothing
End Sub