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

use strict use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Word'; $Win32::OLE::Warn = 2; # Throw Errors, I'll catch them my $Word = Win32::OLE->GetActiveObject('Word.Application') || Win32::OLE->new('Word.Application', 'Quit'); foreach $rate($Word->ActiveDocument->Selection->{Text}){ if ($rate =~ /2M/){ print $rate; }}
i wanted search all content of the activedocument for
extracting out the keyword "2M" , but failed.
$Word->ActiveDocument->Selection->{Text}; $line1 = $Word->Selection->wdline(2)->{Text}; pirnt $line1;
i want to extract the second line of the activedocument the print it out, but failed

$Word->ActiveDocument->Range($Word->ActiveDocument->text)->Select; my $Str = $Word->Selection->Text; print( "Selection=$Str.\n" );
these lines failed.

i am fairly new in perl, i had tried

foreach $rate($Word->Selection->WholeStory) foreach $rate($Word->Content->Text)
all failed.

could you please correct me, i know my codes are has problems, but i have no idea about it.

update (broquaint): added formatting

edited: Mon May 12 14:27:16 2003 by jeffa - title change (was: ole word questions)

Replies are listed 'Best First'.
Re: Win32::OLE word questions
by WhiteBird (Hermit) on May 12, 2003 at 16:21 UTC
    I don't know if it's a typo, or a cut and past error, but your use strict statement is missing the semicolon at the end ;

    Since you are using strict, you will need to declare your variable, $rate with "my" as in:
    my $rate;
    You say it all fails. Do you get any error messages to help indicate where it is failing? One error that I get is "Does not support a collection" in METHOD/PROPERTYGET", referring to the line:
    foreach $rate($Word->ActiveDocument->Selection->{Text}
    And also "Can't use an undefined value as a HASH reference". You might start by looking at your syntax in that bit of code.
    What is your end-goal for this script?

    Have you read the Win32::OLE documentation at ASPN?

      sorry, the first line should be #use strict and the semicolon was exist.
      since the program doesnot work that i am still revise it very often,
      my end-goal is that
      foreach $rate($Word->ActiveDocument->Selection->{Text}){ #get all content of the activedocument
      if ($rate =~ /2M/){ #if $rate could match the keyword
      print $rate; }} #print it out

      $Word->ActiveDocument->Selection->{Text};
      $line1 = $Word->Selection->wdline(2)->{Text}; #get or extract all the words of the second line of the active document
      pirnt $line1;

      i have read win32::ole documentation, but this is the first time i have written the ole word script, i am fairly strange at the ole processing. i know my problem is the method invoking, i have revised more then ten times, i have no idea where the error is.
Re: Win32::OLE word questions
by Zero_Flop (Pilgrim) on May 13, 2003 at 07:07 UTC
    rootstock -

    use strict;
    use Win32::OLE qw(in with);
    use Win32::OLE::Const 'Microsoft Word';

    $Win32::OLE::Warn = 2; # Throw Errors, I'll catch them
    my $Word = Win32::OLE->GetActiveObject('Word.Application') || Win32::OLE->new( 'Word.Application', 'Quit' );

    - Ok you have opened the word application, but you have not opened a document. When you open word normally word will open a blank document assuming you want to work on something new. When you do this through OLE word will not open the document for you.

    foreach $rate ( $Word->ActiveDocument->Selection->{Text} ) {
    if ( $rate =~ /2M/ ) {
    print $rate;
    }
    }

    - AFAIK word will not auto incrament the line of text for you as you want here. You will either have to first make a text selection, then perform your test, then select the next line of text through a process of offsets. ie text = first 10 Char, then text = 11 to 20 char. ** OR better yet use words own Find function to do the work for you. check out.
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/modcore/html/deovrthefindreplacementobjects.asp
    and
    http://aspn.activestate.com/ASPN/Mail/Message/perl-win32-users/1536977
    or
    http://www.suite101.com/article.cfm/perl/89607

    Your BEST tool for starting out is words own macros. Decide on what you want to automate, then turn on words macro and walk through it. You will then have a script in VBA that will perform what you want. Then just translate it to perl. You can get help with the translation from a script VBA2Perl written by Matthew Musgrove. It is a beginning script, but it may give you some idea on how to do the translation

    Why translate from VBA to perl? VBA is not as powerful at manipulating text and perl can interface with more applications then VBA. Try running the VBA against a directory of files, or import/export data between non-office apps and word or excel.

    To the monks: sorry for the links not linked, I can't find were to do that. This really should be automated ;)
      thanks for responding.
      use Win32::OLE; # Object Linking and Embed
      use Win32::OLE::Const 'Microsoft Word'; # Defines constants word k
      my $Word = Win32::OLE->GetActiveObject('Word.Application') || Win32::OLE->new('Word.Application', 'Quit');
      $a= $Word->Selection->Tables(1)->Cell(2,2)->Range->{Text};
      print $a;
      above codes worked
      so i thought
      $a= $Word->Selection->wdline(1)->Range->{Text};
      could work as well, but seemed the wdline(1) is a undefined value, i have been checking perl\html\OLE-Browser about the ole value, but have not work out the problems yet.