Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hi sanPerl,

The code $Word->Selection->Text="This is the inserted text" Looks wrong to me, do you want to select or type text? If you want to type text I think it should be in the form of TypeText("your text goes here"). The code I posted below is untested (I don't have access to test this at the moment) but I think it should work: Tested and working, see my updated at the bottom of this post.
use strict; use warnings; use Win32::OLE; use Win32::OLE::Const 'Microsoft Word'; use Cwd; my ($file) = @ARGV; my $cwd_path = getcwd(); $cwd_path=~ s/\//\\/gs; my $file_name = "$cwd_path\\$file"; my $Word = Win32::OLE->new('Word.Application'); $Word->{'Visible'} = 1; my $document = $Word->Documents->Open($file_name) || die("Unable to op +en document", Win32::OLE->LastError()); my $Selection = $Word->Selection; $Selection->HomeKey (wdStory); $Selection->TypeText("This is the inserted text"); $Selection->Collapse (wdCollapseEnd); $Selection->TypeParagraph; $Selection->TypeText("This is the inserted text 2"); $Selection->Collapse (wdCollapseEnd); $Selection->TypeParagraph; $Word->Documents($file_name)->Save(); $Word->Quit();

You should check out the Win32::OLE documentation, under Hints for Microsoft Office automation it reads:

'The object model for the Office applications is defined in the Visual Basic reference guides for the various applications. These are typically not installed by default during the standard installation.'

If you are unsure of the object model surf over to http://msdn.microsoft.com and have a look around.

ActiveState have an article How to convert a VBA macro to Perl which should be worth a read.

Update:I just tested the VB macro in Word 2003 against the code I posted above. To me it looks like the document produced by the VB code is the same as the document produced by the Perl code.

Hope this helps.

Martin

In reply to Re: VBA to Perl by marto
in thread VBA to Perl by sanPerl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-20 09:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found