Solved

Win32::OLE(0.1702) error 0x800706be: "The remote procedure call failed"

Posted on 2009-04-08
5
2,228 Views
Last Modified: 2012-05-06
I am writing a small perl code for opeining two excel files simultaneously.  When I ran the script it's opening the first file successfully and when it came to open second file. it's throwing the above mentioned error.
#use strict;
use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Excel';
$Win32::OLE::Warn = 3;
sub getdata;
$path1 = "C:\\edu\\Test.xls"; #first file
$pointer = &getdata($path1);
foreach $value1 (@$pointer) {
	foreach $value2 (@$value1){
		print $value2;
		print "\n";
	}
}
 
$path2 = "c:\\edu\\Test2.xls"; #second file
$pointer1 = &getdata($path2);
foreach $value3 (@$pointer1) {
	foreach $value4 (@$pointer2) {
		print $value4;
		}
	}
sub getdata {
my ($path) = @_;
 # get already active Excel
my $Excel = Win32::OLE->GetActiveObject('Excel.Application')
    || Win32::OLE->new('Excel.Application','quit');                                                      # application or open new
my $Book = $Excel->Workbooks->Open($path); # open Excel file
my $Sheet = $Book->Worksheets(1);                     # select worksheet number 1
my $array = $Sheet->Range("A1:B2")->{'Value'};        # get the contents
$Book->Close;
return ($array);
}

Open in new window

0
Comment
Question by:deshaw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 25

Accepted Solution

by:
lwadwell earned 500 total points
ID: 24104385
Hi deshaw,

I think your problem is related to creating the WIN32::OLE object inside the sub-routine ... after it is first called it is destroyed on exit and that it cannot be used again.  Possibly by itself doing an Unitialize.

From WIN32::OLE help page:
"
Win32::OLE->Uninitialize
    The Uninitialize() class method uninitializes the OLE subsystem. It also destroys the hidden top level window created by OLE for single threaded apartments. All OLE objects will become invalid after this call! It is possible to call the Initialize() class method again with a different apartment model after shutting down OLE with Uninitialize().
"

I reworked your script to create the OLE object outside the sub-routine and pass it in and it seems to be fine ...


lwadwell
#use strict;
use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Excel';
$Win32::OLE::Warn = 3;
sub getdata; 
my $Excelobj = Win32::OLE->GetActiveObject('Excel.Application')
        || Win32::OLE->new('Excel.Application','quit');                                                      # application or open new 
$path1 = "c:\\edu\\Test.xls"; #first file
$pointer = &getdata($Excelobj,$path1);
foreach $value1 (@{$pointer}) {
      foreach $value2 (@{$value1}){
            print $value2;
            print "\n";
      }
}
 
$path2 = "c:\\edu\\Test2.xls"; #second file
$pointer1 = &getdata($Excelobj,$path2);
foreach $value3 (@{$pointer1}) {
      foreach $value4 (@{$value3}) {
            print $value4;
            }
      }
sub getdata {
    my ($Excel,$path) = @_;
    # get already active Excel
    my $Book = $Excel->Workbooks->Open($path); # open Excel file
    my $Sheet = $Book->Worksheets(1);                     # select worksheet number 1
    my $array = $Sheet->Range("A1:B2")->{'Value'};        # get the contents
    $Book->Close;
    return ($array);
}

Open in new window

0
 
LVL 1

Author Comment

by:deshaw
ID: 24107360
It's working fine. Thanks.
0
 
LVL 1

Author Comment

by:deshaw
ID: 24107435
One more question. Where can we find more information about what win32::OLE is exactly doing and how it is querying information.
0
 
LVL 25

Expert Comment

by:lwadwell
ID: 24112007
deshaw,

I do not know much more than what is written up in its man page http://search.cpan.org/~jdb/Win32-OLE-0.1709/lib/Win32/OLE.pm, sorry.  It is using Microsoft's OLE extensions - perhaps other experts can help or search the www.

lwadwell
0
 
LVL 39

Expert Comment

by:Adam314
ID: 24126622
When you use Win32::OLE like this, you are calling actual Excel functions.  You can look at the Excel help for details on what the functions do in Excel.
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Perl script to parse xml file 2 144
perl to convert excel to csv 3 341
problem with using the glob function 1 84
Vb script to unzip a files and rename the files 12 131
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question