Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: VBA to Perl

by marto (Archbishop)
on Feb 10, 2006 at 12:01 UTC ( #529333=note: print w/replies, xml ) Need Help??


in reply to VBA to Perl

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

Replies are listed 'Best First'.
Re^2: VBA to Perl
by Samy_rio (Vicar) on Mar 10, 2006 at 09:56 UTC

    Hi marto.

    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 $file_name = "c:\\entityandsymbol.doc"; 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();

    I am getting the error as follows:

    Unable to open documentOLE exception from "Microsoft Word": Could not open macro storage. Win32::OLE(0.1701) error 0x800a175d in METHOD/PROPERTYGET "Open" at array.pl line 4630.

    But the word file opening corectly without modification which I did in coding.

    Suggest me in correct way.

    Updated: I found the solution for the above problem as per "http://www.blink.com.au/tech/trouble.htm" information.

    Could Not Open Macro Storage Error (Run-time error 5981)

    When running BLINK you can get the error message "Could not open macro storage". This is caused by either not having Service Pack 1 installed with Microsoft® Office, or because the Visual Basic and Microsoft® Word installation has become corrupted.

    To fix it you will need to either install Service Pack 1, if it has not been installed before, or reinstall Microsoft® Word.

    I am reinstalled the Microsoft Word then the above code working fine.

    Regards,
    Velusamy R.


    eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

      Hi Samy_rio,

      Your script runs fine for me.

      Google on: Microsoft Office "Could not open macro storage"

      The results indicate there could be a problem with your Office setup.

      Sorry not to be able to help more.

      John

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://529333]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (11)
As of 2019-10-17 13:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?