O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  


 
Buy the book!
Word Hacks
By Andrew Savikas
November 2004
More Info

HACK
#41
Swap Revision Authors
This hack shows you how to edit the information Word stores when you edit a document using the Track Changes feature
[Discuss (0) | Link to this hack]

In addition to marking revisions in a document, Word's Track Changes feature remembers who made the revisions. It lets you view up to eight different revision authors in a single document, each of whose changes are displayed in a different color.

TIP

Select Tools→Options and click the Track Changes tab to modify how Word displays revisions.

But what if you want to modify the name of the author of a particular set of revisions? For example, say you took some work home for the weekend and accidentally did your editing while logged into your computer as one of your kids. Now all your revisions appear as though your teenage son made the changes. Many coworkers would be forgiving, but a client would hardly look kindly on this error.

Unfortunately, you can't modify a revision author from VBA. It does have a Revision object with an Author property, but the property is read-only, meaning you can't give it a different value.

To make the change, you'll need to get the document into a format that takes it out of Word's control, such as RTF (Rich Text Format). When you save a document as an RTF file, you retain the revision information. You can then edit the RTF file with any standard text editor, such as Notepad.

Here's how to change the author of a set of revisions in a Word file.

First, select File→Save As and choose Rich Text Format in the "Save as type" field to save the file as an .rtf file. Next, open the file with a text editor such as Notepad.

TIP

You can find many free text editors available on the Internet with a lot more to offer than Notepad. Check out http://www.crimsoneditor.com for one such free editor.

To locate the part of the file that contains the names of the revision authors, do a search in the file for the following:

{\*\revtbl

You will see a list of revision authors following the string characters, as shown in . The first entry in the list is always Unknown, which you should leave as is. If you edit any of the other names in the list, all revisions attributed to that name will show the change when you open the document in Word.

Figure 1. The list of revision authors inside an RTF file

After you make the change to the RTF file, save it, and then open it in Word. You can now select File→Save As and return it to the native .doc format.

TIP

Be careful when you edit the RTF file. Word (and any other program that reads RTF files) is very sensitive to the correct positioning of those braces. Make sure you don't accidentally delete one of the braces when you edit the name. If you do, Word may not be able to open the file.

Hacking the Hack

Editing RTF files by hand is tricky business. If you regularly swap revision authors in a document, a Perl script can take over the dirty work.

TIP

You can download Perl for a Windows machine for free from http://www.activestate.com.

The following script requires the RTF::Tokenizer module. If you use the ActiveState distribution of Perl, you can use the Perl Package Manager, available from the ActivePerl entry on your Start menu, to install RTF::Tokenizer.

#!/usr/bin/perl

use strict;
use Getopt::Long;
use RTF::Tokenizer;

my %opts = ( );
GetOptions (\%opts, 'from=s', 'to=s');

my $filename = shift;

die "Please provide an rtf file to parse.\n" unless $filename;

my $tokenizer = RTF::Tokenizer->new( file => $filename);

while( my ( $type, $arg, $param ) = $tokenizer->get_token( ) ){
    last if $type eq 'eof';

    if($type eq 'control' and $arg eq 'revtbl') {
        my $match = 0;
        put($type, $arg, $param) if $opts{from} and $opts{to};

        my $brace = 1;

        while($brace > 0){
            my @attr = $tokenizer->get_token( );

            $brace++ if $attr[0] eq 'group' and $attr[1] == 1;
            $brace-- if $attr[0] eq 'group' and $attr[1] == 0;

            if( $attr[0] eq 'text') {
                $attr[1] =~ s/;$//;

                if( $opts{from} and $opts{to} ){
                    if( $opts{from} eq $attr[1] ) {
                        $attr[1] = $opts{to};
                        $match = 1;
                    }

                    $attr[1] .= ';';
                    put( @attr);
                } else {
                    print $attr[1], "\n" unless $attr[1] eq 'Unknown';
                }
            } else {
                put(@attr) if $opts{from} and $opts{to};
            }
        }

        if($opts{from} and $opts{to} and !$match) {
            print STDERR "The author $opts{from} was not found 
                              in the document!\n";
        }
    } else {
        put($type, $arg, $param) if $opts{from} and $opts{to};
    }
}

sub put {
    my ($type, $arg, $param) = @_;

    if( $type eq 'group' ) {
        print $arg == 1 ? '{' : '}';
    } elsif( $type eq 'control' ) {
        print "\\$arg$param"; 
    } elsif( $type eq 'text' ) {
        print "\n$arg"; 
    }
}

Save the script as "authorswap.pl" and put it in the same folder as the RTF file. Run it at a DOS prompt without any arguments to get a list of the revision authors in the document, as shown below:

> perl authorswap.pl MyDoc.rtf
> Brett Johnson
  Rael Dornfest

To replace one revision author with another, use the to and from options, as shown below. Place the names inside quotation marks.

> perl authorswap.pl -from "Brett Johnson" -to "Bob Smith" MyDoc.rtf > NewFile.rtf

The file NewFile.rtf will reflect the changes.

Andrew Savikas and Andy Bruno


O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.