#!/Perl/bin/perl use CGI qw(:standard); use CGI::Carp qw(warningsToBrowser fatalsToBrowser); use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Word'; use Win32::OLE::Enum; use strict; print header; print start_html("Processing..."); print h3("Processing your request..."); my $text = param("search_text"); my $type = param("type"); print "You requested a search for the words \"$text\" in the documents where they appear as $type...\n"; my $time = localtime; my $Word = Win32::OLE->GetActiveObject('Word.Application') || Win32::OLE->new('Word.Application', 'Quit'); my $infile = ""; $Word->Documents->Open($infile,{ReadOnly => 1}) || die("Unable to open document ", Win32::OLE->LastError()); $Word->{visible} = 0; $Word->{DisplayAlerts} = 0; my $range = $Word->ActiveDocument->Content; my $current_song; my $flag; my @mytext; my @mystyle; my $compare; my %song; my $song_count = 0; foreach my $word (in $range->Words){ push(@mytext,$word->{Text}); push(@mystyle,$word->{Style}->{NameLocal}); } for (my $x = 0;$x <= $#mytext; $x++) { my $current_song; my $current_writer; my $current_lyrics; if ($mystyle[$x] eq "Heading 1") { my $counter = 0; undef $current_song; while ($mystyle[$x+$counter] eq "Heading 1") { $current_song .= $mytext[$x+$counter]; $counter++; } $x += $counter; chop($current_song); chop($current_song); $song{Title}[$song_count] = $current_song; } elsif ($mystyle[$x] =~ /Songwriter/) { my $counter = 0; undef $current_writer; while ($mystyle[$x+$counter] =~ /Songwriter/) { $current_writer .= $mytext[$x+$counter]; $counter++; } $x += $counter; chop($current_writer); chop($current_writer); $song{Songwriter}[$song_count] = $current_writer; } else { my $counter = 0; undef $current_lyrics; while ($mystyle[$x+$counter] =~ /Lyrics|Chord/) { if ($mystyle[$x+$counter] eq "Lyrics") { $current_lyrics .= $mytext[$x+$counter]; } $counter++; } $song{Lyrics}[$song_count] = $current_lyrics; $song_count++; $x += $counter; } } print "

Matches:
    "; my $matching_song; for (my $x=0;$x<$song_count;$x++) { if (($type eq "Heading 1" and $song{Title}[$x] =~ /$text/i) or ($type eq "Lyrics" and $song{Lyrics}[$x] =~ /$text/i) or ($type eq "Songwriter" and $song{Songwriter}[$x] =~ /$text/i)) { print "
  1. $song{Title}[$x]: $song{Songwriter}[$x]\n"; print "\n"; $matching_song = $x; } } print "
"; print "


Query started at $time"; $time = localtime; print "
Query completed at $time"; print end_html; $Word->Quit(); undef $Word;