#!/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::O
+LE->new('Word.Application', 'Quit');
my $infile = "<Word document here>";
$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 "<br><br>Matches:<ol>";
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 "<li><b>$song{Title}[$x]:</b> <i>$song{Songwriter}[$x]<
+/i>\n";
print "<ul><li><pre>$song{Lyrics}[$x]</pre>\n";
print "<li><a href=\"gen_song.pl?title=$song{Title}[$x]&format
+=doc\">$song{Title}[$x].doc</a>\n";
print "<li><a href=\"gen_song.pl?title=$song{Title}[$x]&format
+=pdf\">$song{Title}[$x].pdf</a></ul>\n";
$matching_song = $x;
}
}
print "</ol>";
print "<br><br><br><i><font color=blue>Query started at $time";
$time = localtime;
print "<br>Query completed at $time";
print end_html;
$Word->Quit();
undef $Word;
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|