Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Win32::OLE formattings

by Anonymous Monk
on Mar 09, 2005 at 02:45 UTC ( [id://437791]=perlquestion: print w/replies, xml ) Need Help??

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

Monks,

Is there any way to find the formattings like bold italic, bold, superscript etc and replace with someother text using Win32::OLE.

Here is my coding,

$rng->find->clearformatting; $rng->find->font->{'Bold'}= "True"; $rng->find->replacement->clearformatting; $rng->find->execute({findtext=>"[0-9]{1,}", wrap=>wdFindContinue, rep +lacewith=>"<bold>^&</bold>", matchwildcards=>'True', replace=>wdRepla +ceAll});

Using the above code, all the numbers are replaced, what i want is, i want to find the numbers that are in bold alone and replace with some tag.

Thanks

Replies are listed 'Best First'.
Re: Win32::OLE formattings
by maa (Pilgrim) on Mar 09, 2005 at 07:56 UTC

    Hi, Anonymous Monk

    this is just a hint, but it would help you receive answers if you explicitly stated that this is MS Word you're talking about, rather than hoping someone recognises the code or spots that wdFindContinue is a Microsoft Word constant.

    As a start, can you show us the code you use to assign $rng, please?

    --
    Mark

      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};

        Your original code...

        I've tried your code in Word (as a user) and it works. For anyone reading this, ^& is whatever Find just found/matched against.

        Recording a macro suggests the code:

        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 $Selection = $word->{Selection}; $Selection->Find->ClearFormatting(); $Selection->Find->Font->{Bold} = True; $Selection->Find->Replacement->ClearFormatting(); with ($Selection->Find->Replacement->Font, Bold => True, Italic => False ); $Selection->Find->{Text} = "[0-9]{1,}"; $Selection->Find->Replacement->{Text} = "<bold>^&</bold>"; $Selection->Find->{Forward} = True; $Selection->Find->{Wrap} = wdFindContinue; $Selection->Find->{Format} = True; $Selection->Find->{MatchCase} = False; $Selection->Find->{MatchWholeWord} = False; $Selection->Find->{MatchWildcards} = True; $Selection->Find->{MatchSoundsLike} = False; $Selection->Find->{MatchAllWordForms} = False; $Selection->Find->Execute( {Replace=>wdReplaceAll} );
        I've not tested it but hopefully it'll help...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-03-29 09:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found