Your original 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 $rng = $mydoc->{range};
$rng->find->clearformatting;
$rng->find->font->{'Bold'}= "True";
$rng->find->replacement->clearformatting;
$rng->find->execute({findtext=>"[0-9]{1,}", wrap=>wdFindContinue,
replacewith=>"<bold>^&</bold>", matchwildcards=>'True',
replace=>wdReplaceAll});
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...
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|