Leser: 3
|< 1 2 >| | 12 Einträge, 2 Seiten |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
use strict;
use warnings;
#---- Time Module
use POSIX qw(strftime);
use Time::Local;
#---- File Modules
use Cwd;
use File::Basename;
use File::Spec::Win32;
#---- OLE Modules
use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Word'; # fetch list of Word Constants (wdPrintAllDocument, ...
use Win32::OLE::Variant;
#----Interupt Handler
$SIG{ 'INT' } = "_interruptHandler";
#---- Define Boolean Constant Values
use constant TRUE => 1;
use constant FALSE => 0;
use constant CURRENTDIR => cwd(); # Current Directory
#---- Define Pathes
my $sOutputPath = "pdf"; # Output Path Option
my $sDirname = "c:/temp/perl/office2pdf/samples"; # Directory with the files to convert
#---- Various Variables
my $word = undef; # Word instance
my $excel = undef; # Excel instance
my $sInFilename = ""; # Input Filename
my $sOutFilename = ""; # Output Filename
#---- Printer settings
my $sPrinter = "3-Heights(TM) PDF Producer"; # Printer use to 'print to'
#---- Time settings
my $tStart = ""; # Time start
my $tEnd = ""; # Time end
my $tDiff = ""; # Time difference
my $sFile2Convert = ""; # Filename of the file to convert
my $iSheetCount = ""; # Sheet counter
my $sSimpleFilenameTmp = "";
#============================ Main process begin ============================
#----------------------------------------------------------------------------
# Start Process
#----------------------------------------------------------------------------
&_main();
exit;
#----------------------------------------------------------------------------
# Main Process
#----------------------------------------------------------------------------
sub _main(){
#---- Open directory with all Microsoft Office files to convert
opendir(DIR, $sDirname) or die "can't opendir $sDirname: $!";
#---- Loop through every file
while (defined($sInFilename = readdir(DIR))) {
#---- Office conversion
#---- Check if it is a DOC or an XLS file
if( ($sInFilename =~ /.doc/) || ($sInFilename =~ /.xls/) ){
#---- Check & Set Parameters
&_setFilename();
}
if($sInFilename =~ /.doc/) {
#---- Convert Word to PDF
&_convertWord2PDF();
}
elsif($sInFilename =~ /.xls/){
#---- Convert Excel to PDF
&_convertExcel2PDF();
}
}
closedir(DIR);
}
#============================= Main process end =============================
#----------------------------------------------------------------------------
# Word to PDF
#----------------------------------------------------------------------------
sub _convertWord2PDF(){
my $docTmp = undef;
my $CurrentPrinterTmp = "";
#---- Check for Microsoft Word
eval {$word = Win32::OLE->GetActiveObject('Word.Application') || Win32::OLE->new('Word.Application')};
if ($@){
&_printErrorMsg("E999: Word not installed, leaving...\n");
exit -999;
}
#---- Set Microsoft Word Warning level
eval {Win32::OLE->Option(Warn => 3)};
if ($@){
&_printErrorMsg("E997: Error when setting Warning level, leaving...\n");
exit -997;
}
#---- Open file
eval {$docTmp = $word->{'Documents'}->Open($sInFilename)};
if ($@){
&_printErrorMsg("E996: Not able to open " . $sInFilename . ", leaving...\n");
exit -996;
}
#---- Set needed Printer
eval {$word->{'ActivePrinter'} = $sPrinter};
if ($@ || substr( $word->{'ActivePrinter'}, 0, length($sPrinter)) ne $sPrinter){
&_printErrorMsg("E995: Not able to set " . $sPrinter . " as printer, leaving...\n");
exit -995;
}
#---- Print file
$docTmp->PrintOut({
PrintToFile => 1,
OutputFileName => $sOutFilename,
Background => 0,
Append => 0,
Range => wdPrintAllDocument,
Item => wdPrintDocumentContent,
Copies => 1,
PageType => wdPrintAllPages,
});
#--- Force close without saving
eval {$docTmp = $word->{'Documents'}->Close({ SaveChanges => wdDoNotSaveChanges })};
if ($@){
&_printErrorMsg("E994: Not able to close " . $sInFilename . ", leaving...\n");
exit -994;
}
#---- Do not change the following 3 lines or delete them.
#---- If the order is change or one of the statement is missing, only ONE DOC will be convert.
#---- After that a error comes up.
undef $docTmp;
$word->Quit;
undef $word;
}
#----------------------------------------------------------------------------
# Excel to PDF
#----------------------------------------------------------------------------
sub _convertExcel2PDF(){
my $xlsTmp = undef;
my $CurrentPrinterTmp = "";
#---- Check for Microsoft Excel and use the active Excel application or open new
eval {$excel = Win32::OLE->GetActiveObject('Excel.Application') || Win32::OLE->new('Excel.Application', 'Quit')};
if ($@){
&_printErrorMsg("E999: Excel not installed, leaving...\n");
exit -999;
}
#---- Set Microsoft Excel Warning level
eval {Win32::OLE->Option(Warn => 3)};
if ($@){
&_printErrorMsg("E997: Error when setting Warning level, leaving...\n");
exit -997;
}
#---- Open file
eval {$xlsTmp = $excel->{'Workbooks'}->Open($sInFilename)};
if ($@){
&_printErrorMsg("E996: Not able to open " . $sInFilename . ", leaving...\n");
exit -996;
}
#---- Set needed Printer
eval {$excel->{'ActivePrinter'} = $sPrinter};
if (substr( $excel->{'ActivePrinter'}, 0, length($sPrinter)) ne $sPrinter){
&_printErrorMsg("E995: Not able to set " . $sPrinter . " as printer, leaving...\n");
exit -995;
}
#---- Count the sheets within the Excel file
$iSheetCount = $xlsTmp->Sheets->Count();
#---- Loop to print all sheets within the Excel file
for(my $iSheetNo = 1; $iSheetNo <= $iSheetCount; $iSheetNo++) {
#---- Set Output Filename
$sOutFilename = $sOutputPath . "\\" . $sSimpleFilenameTmp . "(" . $xlsTmp->Worksheets($iSheetNo)->Name . ").pdf";
#---- Print file
$xlsTmp->Worksheets($iSheetNo)->PrintOut({
PrintToFile => 1,
PrToFileName => $sOutFilename,
Copies => 1,
});
}
$xlsTmp->Close;
#--- Force close without saving
eval {$xlsTmp = $excel->{'Workbooks'}->Close()};
if ($@){
&_printErrorMsg("E994: Not able to close " . $@ . " " .$sInFilename . ", leaving...\n");
exit -994;
}
undef $xlsTmp;
}
#----------------------------------------------------------------------------
# Set Filenames
#----------------------------------------------------------------------------
sub _setFilename(){
#---- Fetch Input Filename
$sInFilename = $sDirname."/".$sInFilename;
$sSimpleFilenameTmp = fileparse($sInFilename, qr{\..*});
#---- Set Output Path & Filename
$sOutputPath = &_getWin32Path($sOutputPath, TRUE); # remove last Slash/Backslash ../ and so on from Path
$sOutFilename = $sOutputPath . "\\" . $sSimpleFilenameTmp . ".pdf"; # Output Filename
}
#----------------------------------------------------------------------------
# Get Path without last Slash/Backslash
#----------------------------------------------------------------------------
sub _getWin32Path(){
(my $sPathPar) = $_[0];
(my $bNOFilePar) = $_[1];
(my $sDriveTmp, my $sDirTmp, my $sFileTmp) = File::Spec::Win32->splitpath($sPathPar, $bNOFilePar);
if ($sDriveTmp eq ""){
$sPathPar = CURRENTDIR . "\\" . $sPathPar;
}
return File::Spec::Win32->canonpath($sPathPar);
}
#----------------------------------------------------------------------------
# Perl Interrupt Handler
#----------------------------------------------------------------------------
sub _interruptHandler() {
$SIG{ 'INT' } = "IGNORE";
&_printErrorMsg("E001: Unknown Error, leaving...\n");
exit -1;
}
#----------------------------------------------------------------------------
# Protocol and Print Message
#----------------------------------------------------------------------------
sub _printErrorMsg(){
(my $sMsgPar) = $_[0];
$sMsgPar = &_getFormattedMsg($sMsgPar);
print $sMsgPar;
return;
}
#----------------------------------------------------------------------------
# Return formatted Message (with timestamp)
#----------------------------------------------------------------------------
sub _getFormattedMsg(){
(my $sMsgPar) = $_[0];
$sMsgPar = "[" . strftime("%Y.%m.%d_%H:%M:%S", localtime()) . "] " . $sMsgPar;
return $sMsgPar;
}
__END__
use Win32::OLE::Const 'Microsoft Word'
use Win32::OLE::Const ('Microsoft Word','Microsoft Excel')
Quotec:\temp\perl\office2pdf>office2pdf_short.pl
No type library matching "Microsoft Word" found at c:\temp\perl\office2pdf\office2pdf_short.pl line 16
Win32::OLE(0.1707): GetOleTypeLibObject() Not a Win32::OLE::TypeLib object at C:/Perl/lib/Win32/OLE/Const.pm line 49.
Bareword "wdPrintAllDocument" not allowed while "strict subs" in use at c:\temp\perl\office2pdf\office2pdf_short.pl line
128.
Bareword "wdPrintDocumentContent" not allowed while "strict subs" in use at c:\temp\perl\office2pdf\office2pdf_short.pl
line 128.
Bareword "wdPrintAllPages" not allowed while "strict subs" in use at c:\temp\perl\office2pdf\office2pdf_short.pl line 12
8.
Bareword "wdDoNotSaveChanges" not allowed while "strict subs" in use at c:\temp\perl\office2pdf\office2pdf_short.pl line
139.
Execution of c:\temp\perl\office2pdf\office2pdf_short.pl aborted due to compilation errors.
|< 1 2 >| | 12 Einträge, 2 Seiten |