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

Hi,
I need to use Win32::Ole to select a 'paragraph' so that I can format the text. The code I have for displaying the text I want to format is:
use Win32::OLE; my $word = Win32::OLE->new('Word.Application'); $word->{Visible} = 1; my $document=$word->Documents->Add; $word->Selection->TypeText("Some Text");
I would like to select "Some text" as an entire line of the document.
Cheers

Replies are listed 'Best First'.
Re: Win32::Ole selection
by c-era (Curate) on Dec 11, 2001 at 00:48 UTC
    I'm not sure exactly how you want to select the text, but here is one way:
    use Win32::OLE; use Win32::OLE::Const 'Microsoft Word'; my $word = Win32::OLE->new('Word.Application'); $word->{Visible} = 1; my $document=$word->Documents->Add; $word->Selection->TypeText("Some Text"); $word->Selection->HomeKey(wdLine); $word->Selection->EndKey(wdLine, wdExtend)
    P.S. You can easily findout what you need to do by using word's record macro feature.
      A more targeted way of doing this (which allows for inserting and selecting text at arbitrary locations in the file) is:
      use Win32::OLE; use Win32::OLE::Const 'Microsoft Word'; my $str = "Some Text"; my $word = Win32::OLE->new('Word.Application'); $word->{Visible} = 1; my $document=$word->Documents->Add; $word->Selection->TypeText($str); $word->Selection->MoveLeft(wdCharacter, length $str, wdExtend);
      That way would work, but what I'm trying to do is select a known line on the document. In VBA, a command like  ActiveDocument.Paragraphs(1).range can be used to select a specific line on the document (in this case the 1st) but I can't seem to be able to convert this command ....
      Thanks anyway.
        Solved it!
        use Win32::OLE; my $word = Win32::OLE->new('Word.Application'); $word->{Visible} = 1; my $document=$word->Documents->Add; $word->Selection->TypeText("Some Text"); $word->Selection->Paragraphs("1"); $document->Select->ActiveDocument->Paragraphs(1)->range;
        Cheers.