从两个脚本同时打开不同文件的Perl Excel OLE错误 [英] Perl Excel OLE error opening different files from two scripts simultanesouly

查看:134
本文介绍了从两个脚本同时打开不同文件的Perl Excel OLE错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试从单独的perl脚本中打开数据并将数据写入两个不同的excel文件,但又同时 .

Trying to open and write data into two different excel files from separate perl scripts but simultaneously.

一个接一个地运行不会引发任何错误.但是同时运行它时,其中一个脚本会终止,并显示以下错误.

It doesn't throw any error if run one after other. But while running simultaneously it one of the script terminates with following error.

 entering 
 No Error 
 excel new created! 
 Win32::OLE(0.1709) error
 0x8001010a: "The message filter indicated that the application is
 busy"
   in METHOD/PROPERTYGET "Workbooks" at TwoExcelFiles.pl line 29. came out. sleeping 20 C:\Users\s.mailappan\Desktop\Test>

Perl脚本1:

# ================
# Modules Required
# ================
use Win32::OLE qw(in with);                 # OLE Automation extensions
use Win32::OLE::Const 'Microsoft Excel';    # Extract constant definitions from TypeLib
use Cwd;                                    # Get pathname of current working directory
use File::Copy;                             # Copy files or filehandles

$Win32::OLE::Warn = 3;

    # Open Excel application
    # my $Excel2 = Win32::OLE->GetActiveObject('Excel.Application')
     # || Win32::OLE->new( 'Excel.Application', 'Quit' );

    # use existing instance if Excel is already running

    printf "\nentering";
        eval {$Excel2 = Win32::OLE->GetActiveObject('Excel.Application');printf "\nNo Error";};
        die "Excel not installed" if $@;
        printf "\nexcel new created!";
        unless (defined $Excel2) {
        printf "\ncreating new excel on file-2";
            $Excel2 = Win32::OLE->new('Excel.Application', sub {$_[0]->Quit;})
                    or die "Oops, cannot start Excel";
        }
        printf "\ncame out. sleeping 20";
    # Open workbook
    # my $Book1 = $Excel2->Workbooks->Open("C:\\Users\\s.mailappan\\Desktop\\Test\\CoreNetwork_Master_Excel_WSS_Oma.xlsx");
    my $Book2 = $Excel2->Workbooks->Open("C:\\Users\\s.mailappan\\Desktop\\Test\\CoreNetwork_Master_Excel_WSS_Che.xlsx");
    # $Book1->Save;
    # $Book1->Close();

    $Book2->Save;
    $Book2->Close();
    sleep(20);
    printf "\nsleep done";
    $Excel2->Quit();

Perl脚本2:

# ================
# Modules Required
# ================
use Win32::OLE qw(in with);                 # OLE Automation extensions
use Win32::OLE::Const 'Microsoft Excel';    # Extract constant definitions from TypeLib
use Cwd;                                    # Get pathname of current working directory
use File::Copy;                             # Copy files or filehandles

$Win32::OLE::Warn = 3;

    # Open Excel application
    # my $Excel1 = Win32::OLE->GetActiveObject('Excel.Application')
     # || Win32::OLE->new( 'Excel.Application', 'Quit' );

    # use existing instance if Excel is already running

    printf "\nentering";
        eval {$Excel1 = Win32::OLE->GetActiveObject('Excel.Application');printf "\nNo Error";};
        die "Excel not installed" if $@;
        printf "\nexcel new created!";
        unless (defined $Excel1) {
        printf "\ncreating new excel on file-2";
            $Excel1 = Win32::OLE->new('Excel.Application', sub {$_[0]->Quit;})
                    or die "Oops, cannot start Excel";
        }
        printf "\ncame out. sleeping 20";
    # Open workbook
    my $Book1 = $Excel1->Workbooks->Open("C:\\Users\\s.mailappan\\Desktop\\Test\\CoreNetwork_Master_Excel_WSS_Oma.xlsx");
    # my $Book2 = $Excel1->Workbooks->Open("C:\\Users\\s.mailappan\\Desktop\\Test\\CoreNetwork_Master_Excel_WSS_Che.xlsx");
    $Book1->Save;
    $Book1->Close();

    # $Book2->Save;
    # $Book2->Close();
    sleep(20);
    printf "\nsleep done";
    $Excel1->Quit();

推荐答案

如stenci所示-代替使用Win32 :: OLE-> GetActiveObject,而使用Win32 :: OLE->如下定义的新作品.

As hinted by stenci - instead of using Win32::OLE->GetActiveObject, using Win32::OLE->new works as defined below.

unless (defined $Excel1_Parse4G) {
  $Excel1_Parse4G = Win32::OLE->new('Excel.Application', sub {$_[0]->Quit;})
  or die "Oops, cannot start Excel";
}   
my $Book1 = $Excel1_Parse4G->Workbooks->Open("C:\\path\file1.xls");

unless (defined $Excel2_Parse4G) {
  $Excel2_Parse4G = Win32::OLE->new('Excel.Application', sub {$_[0]->Quit;})
  or die "Oops, cannot start Excel";
}   
my $Book2 = $Excel2_Parse4G->Workbooks->Open("C:\\path\file2.xls");

这篇关于从两个脚本同时打开不同文件的Perl Excel OLE错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆