-3

I am actually trying to change the color index for the first word with braces in an array so they show up in the right color in Word 2003.

For example, if I have an array like this:

@array="
        (This) is (perl),
         perl is a great (language),
         we can do anything with perl,
         (perl) feels us great."

I need the colour of the first word that is inside the parentheses (), i.e. (This) and (perl) including () to be in red and rest of the contents in black. and print the entire contents of the array in MS Word 2003:

I'm using Win32::OLE and Windows XP. This array is just an example the contents of an array will change but first word with braces has to be printed in red.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
User1611
  • 1,081
  • 4
  • 18
  • 27
  • "feels us great" sounds sexual and makes no sense otherwise. Even then it's a stretch. I know you are not a native English speaker, so don't say that, unless you want people to laugh at you. "(perl) makes us feel great", or "(perl) makes me feel great" or "(perl) feels great" are acceptable. – Richard Anthony Hein Mar 17 '10 at 13:49
  • You know, Sinan, that he's not going to change his behavior as long as you continue to reward him with excellent answers. :) – brian d foy Mar 17 '10 at 14:46
  • @brian True, very true. However, note that my code also colors `(language)` red on purpose. Plus, I try to follow the principle that the site is about questions and answers, not individual posters. So, I actually chose to wait about 10 hours before I answered his **urgent** question. – Sinan Ünür Mar 17 '10 at 16:36
  • hey guys tell me what am making wrong? i have accepted all the answers that is relevant to my ques.. and of course am not the guy to just look out for just answers i have tried enough to myself and if and only if i couldnt, i post my questions here.i am sorry if my things hurts you all.i think so am not good enough to post the questions here as you all expect.sorry guys – User1611 Mar 17 '10 at 17:38
  • @lokesh Take a look at http://stackoverflow.com/questions/2130836/how-can-i-store-entire-contents-of-a-perl-array-to-a-scalar-variable http://stackoverflow.com/questions/2092784/how-can-i-properly-install-win32guitest http://stackoverflow.com/questions/1124676/how-can-i-extract-data-in-a-word-document-using-perl http://stackoverflow.com/questions/1824240/how-do-i-find-all-lib-directories-under-a-windows-path http://stackoverflow.com/questions/915175/how-can-i-extract-links-from-an-html-file-with-perl and others. I can't see why none of the answers was acceptable. – Sinan Ünür Mar 17 '10 at 19:19

1 Answers1

2
#!/usr/bin/perl

use strict; use warnings;
use Win32::OLE;
use Win32::OLE::Const 'Microsoft Word';
$Win32::OLE::Warn = 3;

my $word = get_app('Word.Application');
$word->{Visible} = 1;

my $doc = $word->Documents->Add;

while ( my $line = <DATA> ) {
    my @chunks = split /(\(\w+\))/, $line;
    my $seen;
    for my $chunk ( @chunks ) {
        my $sel = $word->Selection;
        my $font = $sel->Font;
        if ( $chunk =~ /^\(/ and not $seen) {
            $font->{ColorIndex} = wdRed;
            $seen = 1;
        }
        else {
            $font->{ColorIndex} = wdBlack;
        }
        $sel->TypeText($chunk);
    }
}

sub get_app {
    my ($class) = @_;
    my $app;
    eval {
        $app = Win32::OLE->GetActiveObject($class);
    };

    if ( my $ex = $@ ) {
        die $ex, "\n";
    }

    unless(defined $app) {
        $app = Win32::OLE->new($class, sub { $_[0]->Quit })
            or die "Oops, cannot start '$class': ",
                   Win32::OLE->LastError, "\n";
    }
    return $app;
}

__DATA__
(This) is (perl),
perl is a great (language),
we can do anything with perl,
(perl) feels us great.
Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339