Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

How to Highlight with OLE

by MysticRyuujin (Novice)
on Apr 04, 2014 at 01:22 UTC ( #1081050=perlquestion: print w/replies, xml ) Need Help??
MysticRyuujin has asked for the wisdom of the Perl Monks concerning the following question:

I'm playing with replacing Japanese text with English text in Word documents using Perl. One of the things I want to do is highlight the text that I replace but I can't seem to get highlighting to work. I think the macro is: Selection.Range.HighlightColorIndex = wdYellow

This is the code I'm working with right now and it will successfully replace Japanese text in the word document but highlighting is eluding me...

use strict; use warnings; use utf8; use Win32::OLE; use Win32::OLE qw(CP_UTF8); use Win32::OLE::Const 'Microsoft Word'; Win32::OLE->Option(CP => CP_UTF8); my $word = Win32::OLE->new('Word.Application'); $word->{Visible} = 0; my $document=$word->Documents->Open('c:/perl/scripts/test.doc'); $word->Selection->HomeKey(wdStory); $word->Selection->Find->{'Text'}='すごい'; $word->Selection->Find->Replacement->{'Text'}='Awesome'; $word->Selection->Find->Execute({Replace=>wdReplaceAll}); $word->Selection->HighlightColorIndex(wdYellow); # THIS I DONT KNOW $word->ActiveDocument->Close ; $word->Quit;

Replies are listed 'Best First'.
Re: How to Highlight with OLE
by dasgar (Priest) on Apr 04, 2014 at 17:59 UTC

    I haven't tried to highlight anything in Word via Win32::OLE before, so this is just a guess. If I'm reading your code correctly, you're selecting some text, replacing the 'selection' and then trying highlight the 'selection'. I suspect that the issue is that after replacing the text, the 'selection' that is being acted upon is just the cursor placement at the end of the replacement text. If I'm correct, you'll need to select the replacement text before doing the highlighting.

Re: How to Highlight with OLE
by Anonymous Monk on Apr 04, 2014 at 01:29 UTC

    $word->Selection->HighlightColorIndex(wdYellow); # THIS I DONT KNOW

    Me neither :) what does the documentation say?

      I've searched for "OLE" and "Highlight" a lot but never found anything. BUT I solved my problem...Solution:
      $word->Options->DefaultHighlightColorIndex(wdYellow); $word->Selection->Find->Replacement->{'Highlight'}=1;

        I've searched for "OLE" and "Highlight" a lot but never found anything.

        Where did you search, MSDN right?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1081050]
Approved by boftx
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2018-04-26 01:02 GMT
Find Nodes?
    Voting Booth?