Initial commit
[profile/ivi/openjade.git] / contrib / rtf2doc.pl
1 #! /usr/bin/perl -w 
2 # Copyright (C) 1999 Avi Kivity
3
4 sub usage
5 {
6     print <<__USAGE__;
7 Usage: rtf2doc.pl FILE...
8   Converts one or more MS .rtf FILEs to MS Word .doc files.
9   Requires MS Word 97 or above, plus correct phase of moon.
10 __USAGE__
11 }
12
13 usage(), exit 0 
14     if ($#ARGV < 0) || ($ARGV[0] =~ /^((-h)|(--help))$/);
15
16 use Cwd;
17 use Win32::OLE;
18 use Win32::OLE::Const 'Microsoft Word';
19
20 $cwd = cwd();
21
22 @storytypes = ( 4, 3, 8, 6, 11, 10, 2, 1, 9, 7, 5 );
23 my $Word = Win32::OLE->new('Word.Application', 'Quit');
24 die "Cannot open MS Word\n" unless $Word;
25
26 foreach (@ARGV) {
27     my $src = $_;
28     $src = "$cwd\\$src"
29         unless $src =~ /^(.:\\)/;
30     my $tgt = "$src.doc";
31     my $Doc = $Word->Documents->Open($src);
32     die "Cannot open $src\n" unless $Doc;
33     $Doc->Repaginate();
34     $Stories = $Doc->StoryRanges;
35     for $s (@storytypes) {
36         eval {            
37             local $SIG{__WARN__} = {};    
38             $Story = $Stories->Item($s);
39             if ($Story) {
40                 $Story->Fields->Update();
41                 $Story->Fields->Unlink();
42             }
43         };
44     }
45     $Doc->SaveAs($tgt, 0);
46     $Doc->Close();
47 }
48
49 $Word->Quit();