将所有 cwd 目录添加到一个数组,逐个目录迭代目录并打开 - 仅保存其中的 .xlsm 文件(Windows 10)
Add all cwd directories to an array, iterate directory by directory and open-save only .xlsm files in there (Windows 10)
目标: 将所有目录添加到一个数组中,逐个目录迭代目录并打开-仅保存其中的 .xlsm 文件。 (当它们打开时,自动执行 vba 宏)
错误: 用户代码未捕获异常:打开目录 Z:/Folder1/Projects/Templates/Abschlussbesuch 失败:[= 处的参数无效26=]stammdaten.pl 第 26 行.
目录:
stammdaten.cmd:
@echo off
perl -w stammdaten.pl
pause
代码:
# ------ Module ------ #
use strict;
use warnings;
use diagnostics;
use Cwd;
use OLE;
use Win32::OLE::Const "Microsoft Excel";
use Unicode::UTF8;
#----------------------#
# ------ HAUPTPROGRAMM ------ #
my $excel = CreateObject OLE "Excel.Application";
$excel->{Visible} = 0;
$excel->{DisplayAlerts} = 0;
$excel->{AskToUpdateLinks} = 0;
opendir(OD, cwd) or die "Kann Arbeitsverzeichnis nicht öffnen! $!";
my @verzeichnisse = grep { -d } glob cwd . "/*";
closedir OD;
foreach my $v(@verzeichnisse)
{
my $dir = cwd . "/$v";
opendir(my $verz, $dir) or die "Opening of directory $v failed: $!"; # LINE 26
foreach my $xlsm (<*.xlsm>)
{
open(FH, $xlsm) or die "Die Excel-Mappe $xlsm konnte nicht geoeffnet werden: $!";
my $mappe = $excel->Workbooks->Open($xlsm);
$mappe->Save;
$mappe->Close;
$excel->Quit;
close FH;
}
closedir($verz);
}
#-----------------------------#
# ------ ENDE ------ #
exit 0;
请尝试以下代码(它应该产生相同的结果)
# ------ Module ------ #
use strict;
use warnings;
use diagnostics;
use Cwd;
use OLE;
use Win32::OLE::Const "Microsoft Excel";
use Unicode::UTF8;
#----------------------#
# ------ HAUPTPROGRAMM ------ #
my $excel = CreateObject OLE "Excel.Application";
$excel->{Visible} = 0;
$excel->{DisplayAlerts} = 0;
$excel->{AskToUpdateLinks} = 0;
my $path = cwd;
foreach my $xlsm (glob('*/*.xlsm'))
{
$xlsm = $path . $xlsm;
open(FH, $xlsm) or die "Die Excel-Mappe $xlsm konnte nicht geoeffnet werden: $!";
my $mappe = $excel->Workbooks->Open($xlsm);
$mappe->Save;
$mappe->Close;
$excel->Quit;
close FH;
}
#-----------------------------#
# ------ ENDE ------ #
exit 0;
提示:modern perl 第 139 页 - 建议使用以下形式的打开
open my $fh, '<', $filename
or die "Couldn't open $filename";
....
....
close $fh;
目标: 将所有目录添加到一个数组中,逐个目录迭代目录并打开-仅保存其中的 .xlsm 文件。 (当它们打开时,自动执行 vba 宏)
错误: 用户代码未捕获异常:打开目录 Z:/Folder1/Projects/Templates/Abschlussbesuch 失败:[= 处的参数无效26=]stammdaten.pl 第 26 行.
目录:
stammdaten.cmd:
@echo off
perl -w stammdaten.pl
pause
代码:
# ------ Module ------ #
use strict;
use warnings;
use diagnostics;
use Cwd;
use OLE;
use Win32::OLE::Const "Microsoft Excel";
use Unicode::UTF8;
#----------------------#
# ------ HAUPTPROGRAMM ------ #
my $excel = CreateObject OLE "Excel.Application";
$excel->{Visible} = 0;
$excel->{DisplayAlerts} = 0;
$excel->{AskToUpdateLinks} = 0;
opendir(OD, cwd) or die "Kann Arbeitsverzeichnis nicht öffnen! $!";
my @verzeichnisse = grep { -d } glob cwd . "/*";
closedir OD;
foreach my $v(@verzeichnisse)
{
my $dir = cwd . "/$v";
opendir(my $verz, $dir) or die "Opening of directory $v failed: $!"; # LINE 26
foreach my $xlsm (<*.xlsm>)
{
open(FH, $xlsm) or die "Die Excel-Mappe $xlsm konnte nicht geoeffnet werden: $!";
my $mappe = $excel->Workbooks->Open($xlsm);
$mappe->Save;
$mappe->Close;
$excel->Quit;
close FH;
}
closedir($verz);
}
#-----------------------------#
# ------ ENDE ------ #
exit 0;
请尝试以下代码(它应该产生相同的结果)
# ------ Module ------ #
use strict;
use warnings;
use diagnostics;
use Cwd;
use OLE;
use Win32::OLE::Const "Microsoft Excel";
use Unicode::UTF8;
#----------------------#
# ------ HAUPTPROGRAMM ------ #
my $excel = CreateObject OLE "Excel.Application";
$excel->{Visible} = 0;
$excel->{DisplayAlerts} = 0;
$excel->{AskToUpdateLinks} = 0;
my $path = cwd;
foreach my $xlsm (glob('*/*.xlsm'))
{
$xlsm = $path . $xlsm;
open(FH, $xlsm) or die "Die Excel-Mappe $xlsm konnte nicht geoeffnet werden: $!";
my $mappe = $excel->Workbooks->Open($xlsm);
$mappe->Save;
$mappe->Close;
$excel->Quit;
close FH;
}
#-----------------------------#
# ------ ENDE ------ #
exit 0;
提示:modern perl 第 139 页 - 建议使用以下形式的打开
open my $fh, '<', $filename
or die "Couldn't open $filename";
....
....
close $fh;