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

Hi Monks,

I need some guidance to proceed further.

I want to find and replace the superscipt with some text using Win32::OLE in word doc.

I tried using the following code, but i cant able to find.

use strict; use Win32::OLE; use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Word'; use Constant 'True' => 1; use Constant 'False' => 0; #$Win32::OLE::Warn = 2; # Throw Errors, I'll catch them my $word = Win32::OLE->GetActiveObject('Word.Application') || Win32::OLE->new('Word.Application', 'Quit'); my $mydoc = $word->activedocument; my $rng = $mydoc->{range}; $rng->find->clearformatting; $rng->find->replacement->clearformatting; $rng->find->font->{Superscript}= "True"; $rng->find->execute({findtext=>"[0-9]", wrap=>wdFindContinue, replace +with=>"<sup>^&</sup>", matchwildcards=>'True', replace=>wdReplaceAll} +);

Replies are listed 'Best First'.
Re: how to replace superscript using Win32::OLE
by traveler (Parson) on Mar 08, 2005 at 18:13 UTC
    It worked for me in Word2000. I think maybe you want to make the <sup>...</sup> to be not superscript, but your replacement text does not change the formatting. It does, however, replace the exponent with the new text.

      I want to replace only the superscript numbers alone, but for me all the numbers are changing.

        This seems to work:
        use Win32::OLE::Const 'Microsoft Word'; use Win32::OLE::Const 'Microsoft Office'; use Constant 'True' => 1; use Constant 'False' => 0; #$Win32::OLE::Warn = 2; # Throw Errors, I'll catch them my $word = Win32::OLE->GetActiveObject('Word.Application') || Win32::OLE->new('Word.Application', 'Quit'); my $mydoc = $word->activedocument; my $rng = $mydoc->{range}; $rng->find->clearformatting; $rng->find->replacement->clearformatting; $rng->find->font->{Superscript}= msoTrue; $rng->find->execute({findtext=>"[0-9]", wrap=>wdFindContinue, replacewith=>"<sup>^&</sup>", matchwildcards=>'True', # matchallwordforms => msoFalse, replace=>wdReplaceAll});