Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Win32 OLE change paragraph text color

by cormanaz (Deacon)
on Aug 01, 2016 at 17:53 UTC ( [id://1168945]=perlquestion: print w/replies, xml ) Need Help??

cormanaz has asked for the wisdom of the Perl Monks concerning the following question:

Good day bros. I have some interview transcripts in Word with statements by interviewer and interviewee in successive paragraphs. I want to change the font color of the text in the interviewer paragraphs to a different color. I've verified that the following will open the document and find the interviewer paragraphs:
#!/usr/bin/perl -w use strict; use Win32::OLE; my $document_name = 'C:\Users\boss\worddoc.doc'; my $word = Win32::OLE->GetActiveObject('Word.Application') || Win32::OLE->new('Word.Application','Quit') or die Win32::OLE->LastError(); my $document = $word->Documents->Open($document_name) or die Win32::OLE->LastError(); my $paragraphs = $document->Paragraphs(); my $n_paragraphs = $paragraphs->Count(); for my $p (1..$n_paragraphs) { my $paragraph = $paragraphs->Item($p); my $text = $paragraph->Range->Text(); if ($text =~ /interviewer/i) { print "$text\n\n"; } } $document->close;
But how do I change the text color of these paragraphs?

Replies are listed 'Best First'.
Re: Win32 OLE change paragraph text color
by poj (Abbot) on Aug 01, 2016 at 19:14 UTC
    Try
    #!/usr/bin/perl -w use strict; use Win32::OLE; use Win32::OLE::Const; my $wd = Win32::OLE::Const->Load("Microsoft Word 14.0 Object Library") +; printf "wdRed = %s\n", $wd->{wdRed}; printf "wdGreen = %s\n", $wd->{wdGreen}; my $document_name = 'C:\Users\boss\worddoc.doc'; my $word = Win32::OLE->GetActiveObject('Word.Application') || Win32::OLE->new('Word.Application','Quit') or die Win32::OLE->LastError(); my $document = $word->Documents->Open($document_name) or die Win32::OLE->LastError(); my $paragraphs = $document->Paragraphs(); my $n_paragraphs = $paragraphs->Count(); for my $p (1..$n_paragraphs) { my $paragraph = $paragraphs->Item($p); my $text = $paragraph->Range->Text(); my $font = $paragraph->Range->Font(); if ($text =~ /interviewer/i) { print "$text\n\n"; $font->{'ColorIndex'} = $wd->{wdGreen}; } else { $font->{'ColorIndex'} = $wd->{wdRed}; } } $document->Save; $document->close;

    If the constants don't load just use a color index of 6 for red, 11 for green

    poj
      Works perfectly. Thanks so much!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1168945]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-26 08:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found