none
Formatting Tracked Changes RRS feed

  • Question

  • I have a macro that copys your file that contains some amount of tracked changes, pastes it to a new doc, then changes deletions to be red text and insertions to be blue text.

    I want to be able to restrict this to run within a single table. I have to code to select just the table and the code works when running it for the entire document. But, when I write the method to run it on just the table, it copys the selected table to the new doc but it implements the tracked changes instead of changing thier colors. Any ideas why this isnt wokring properly?

    Here is the code: (the commented out part is just some code i tried but also wasnt working)

        dim wrng as word.range

        'Set wrng = Word.selection.Range
        'If wrng.Information(wdWithInTable) Then
        '    'regardless of cursor position, selects entire table range
        '    wrng.StartOf unit:=Word.WdUnits.wdTable, Extend:=Word.wdExtend
        '    wrng.EndOf unit:=Word.WdUnits.wdTable, Extend:=Word.wdExtend
        'Else
        'Dim wack As VbMsgBoxResult
        'wack = MsgBox("You must select within a table first, please try again.", vbOKOnly)
        'If wack = vbOK Then
        'GoTo ExitHere
        'End If
        'End If
       
        selection.Range.Select
        If selection.Information(wdWithInTable) Then
        selection.StartOf unit:=Word.WdUnits.wdTable, Extend:=Word.wdExtend
        selection.EndOf unit:=Word.WdUnits.wdTable, Extend:=Word.wdExtend
        Else
        Dim wack As VbMsgBoxResult
        wack = MsgBox("You must select within a table first, please try again.", vbOKOnly)
        If wack = vbOK Then
        GoTo ExitHere
        End If
        End If
       
        'selection.WholeStory
        selection.Copy
        'wrng.Copy
        'Documents.Add DocumentType:=wdNewBlankDocument
        Set oNewDoc = Documents.Add
        selection.PasteAndFormat (wdPasteDefault)
        'wrng.Paste
       
        Set oNewDoc = ActiveDocument

        Set doc = ActiveDocument
        doc.TrackRevisions = False
       
        For Each oRevision In oNewDoc.Revisions
            Select Case oRevision.Type
                Case wdRevisionDelete
                    oRevision.Range.Font.StrikeThrough = True
                    oRevision.Range.Font.Color = wdColorRed
                    oRevision.Reject
                Case wdRevisionInsert
                    oRevision.Range.Underline = wdUnderlineSingle
                    oRevision.Range.Font.Color = wdColorBlue
                    oRevision.Accept
              
            End Select
            n = n + 1
        Next
           
        WordBasic.AcceptAllChangesInDoc

    Tuesday, July 12, 2011 3:20 PM

Answers

  • Take a look at this code, it should give you what you're looking for.

    Sub SelectionRange()
      Dim doc As Word.Document
      Dim rng As Word.Range
      
      Set doc = Word.ActiveDocument
      Set rng = Word.Selection.Range
      
      If rng.Information(wdWithInTable) Then
        'regardless of cursor position, selects entire table range
        rng.StartOf unit:=Word.WdUnits.wdTable, Extend:=Word.wdExtend
        rng.EndOf unit:=Word.WdUnits.wdTable, Extend:=Word.wdExtend
        rng.MoveStart unit:=Word.wdCharacter, count:=-1
      End If
      
      If rng.Start = rng.End Then
        'defaults to the entire document
        Set rng = doc.Range
      End If
      
      rng.Select 'only here to visually show selected area during testing
      
      Debug.Print rng.Text 'another testing item
      
      'add code here to format/copy the range
      
    End Sub
    
    

     


    Kind Regards, Rich ... http://greatcirclelearning.com
    • Marked as answer by CTroend Wednesday, July 13, 2011 5:02 PM
    Wednesday, July 13, 2011 3:18 PM

All replies

  • I'm taking a guess that the tracked changes are not held in any of the table markers but they are held at a global level in the document possibly at a Section Break or Last Paragraph mark in the document. By copying just a single table you get what you see ... the final markup.

    You might experiment a bit with it by copying the entire file but then deleting everything but the table and the last paragraph mark or section break and see what results you get.


    Kind Regards, Rich ... http://greatcirclelearning.com
    Tuesday, July 12, 2011 4:05 PM
  • I read that the workaround to get the tracked changes to stay within the table when copying it is to also copy the paragraph before it along with the table. In code; how do I set my range being selected to include the content control that occurs right before the table. Currently my selection parameters are:

        selection.StartOf unit:=Word.WdUnits.wdTable, Extend:=Word.wdExtend
        selection.EndOf unit:=Word.WdUnits.wdTable, Extend:=Word.wdExtend

    Wednesday, July 13, 2011 2:33 PM
  • Take a look at this code, it should give you what you're looking for.

    Sub SelectionRange()
      Dim doc As Word.Document
      Dim rng As Word.Range
      
      Set doc = Word.ActiveDocument
      Set rng = Word.Selection.Range
      
      If rng.Information(wdWithInTable) Then
        'regardless of cursor position, selects entire table range
        rng.StartOf unit:=Word.WdUnits.wdTable, Extend:=Word.wdExtend
        rng.EndOf unit:=Word.WdUnits.wdTable, Extend:=Word.wdExtend
        rng.MoveStart unit:=Word.wdCharacter, count:=-1
      End If
      
      If rng.Start = rng.End Then
        'defaults to the entire document
        Set rng = doc.Range
      End If
      
      rng.Select 'only here to visually show selected area during testing
      
      Debug.Print rng.Text 'another testing item
      
      'add code here to format/copy the range
      
    End Sub
    
    

     


    Kind Regards, Rich ... http://greatcirclelearning.com
    • Marked as answer by CTroend Wednesday, July 13, 2011 5:02 PM
    Wednesday, July 13, 2011 3:18 PM
  • Perfect!

    Thanks Rich, huge help.

    -Chris

    Wednesday, July 13, 2011 5:01 PM