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

hi Perlers, I am learning perl. Trying to get this code to search for this pattern ABCD-4 in a docx file (the pattern either part of another string or by itself) , but its not working . Please help .
use strict; use warnings; use Win32::OLE::Const 'Microsoft Word'; my $file = 'C:\Strawberry\Perl\bin\2test.docx'; my $Word = Win32::OLE->new('Word.Application', 'Quit'); $Word->{'Visible'} = 0; my $doc = $Word->Documents->Open($file) or die "Error opening the docu +ment!\n"; my $paragraphs = $doc->Paragraphs() ; my $enumerate = new Win32::OLE::Enum($paragraphs); while ( defined (my $paragraph = $enumerate->Next()) ) { my $words = Win32::OLE::Enum->new( $paragraph->{Range}->{Words} ); while ( defined ( my $word = $words->Next() ) ) { my $text = $word->{Text}; ## print "$text \n"; if ($text =~ /[A-Z]{4}-[0-9]{1}/){ $paragraph->Select; my $page_number = $Word->Selection->Information(wdActi +veEndPageNumber); print "Found $text in pg $page_number!\n"; } } } $Word->ActiveDocument->Close ; $Word->Quit;
I am using this sample data file
Control HGRF-1(Negotiated), JFHH-1, ECIC-1, ECNK-1 ABDF-4
But I am getting output as
Control HGRF - 1 ( Negotiated ) , J FHIH - 1 ... so on

Replies are listed 'Best First'.
Re: Win32 Pattern issue
by poj (Abbot) on Feb 04, 2017 at 20:11 UTC

    Try searching the whole paragraph

    #!perl use strict; use Win32::OLE; use Win32::OLE::Const 'Microsoft Word'; $Win32::OLE::Warn = 3; my $MSWord = Win32::OLE->new('Word.Application', 'Quit') or die Win32::OLE->LastError(); $MSWord->{Visible} = 0; my $doc = $MSWord->Documents->Open({FileName=>"c:/temp/2test.docx"}) or die Win32::OLE->LastError();; my $para = $doc->Paragraphs() ; my $enum = new Win32::OLE::Enum($para); my $sel = $MSWord->Selection; while ( defined (my $para = $enum->Next()) ) { my $txt = $para->Range->Text; while ( $txt =~ /([A-Z]{4}-[0-9])/g ){ $sel->Find->{Text} = $1; $sel->Find->Execute; my $page_num = $sel->Range->Information(wdActiveEndPageNumber); print "page $page_num .. ".$sel->Text."\n"; $sel->MoveRight({Count=>1,Unit=>wdWord}) } } $MSWord->ActiveDocument->Close ;
    poj
Re: Win32 Pattern issue
by Anonymous Monk on Feb 04, 2017 at 18:52 UTC
    HI me again .actually The script is not able to look for the pattern at all.