Code Comments
Programming Forum and web based access to our favorite programming groups.Long time lurker, first time poster...please be gentle.
I am attempting to move through a directory, pull out only files of a
.fin extension (which is Word document format), save each one as .rtf
(Rich Text Format). I'd also like to be able to run some predefined
macros on the files, but I thought just being able to save was a good
place to start.
Currently I get an error stating 'Can't call method "SaveAs" on an
undefined value'.
If someone could point me to some clearer text on Win32::OLE than the
stuff that ASPN, CPAN and Oreilly's Win32 or Nutshell book I'd greatly
appreciate it. If you could tell me what stupid thing I am missing this
time that would be great too.
Here's the code:
#usr/bin/perl
use Win32::OLE;
use Win32::OLE::Const 'Microsoft Word';
$articleDirName = "c:\\work\\test\\psq\\test\\";
unless (opendir(DIR, $articleDirName)) {
warn "Can't open $articleDirName\n";
closedir(DIR);
exit(1);
}
foreach (readdir(DIR))
{
next if $_ eq '.' || $_ eq '..';
$path = "$articleDirName$_";
if (-f $path){
if ($path =~ /\.fin/i){
$doc = Win32::OLE->GetObject($path);
$fileName = $path . ".rtf";
print "$fileName\n";
$doc->ActiveDocument->SaveAs({FileName => $fileName,
FileFormat => wdFormatRTF})
}
undef $doc;
undef $path;
undef $fileName;
}
}
Post Follow-up to this messageDear Mark,
> Long time lurker, first time poster...please be gentle.
Shame you missed the many comments on strict and
warnings. Consider these mandatory whilst learning:
use strict;
use warnings;
I am not familar with Win32::OLE, but there is certainly
some errors in your code I can show you. And some
suggestions to help.
> $articleDirName = "c:\\work\\test\\psq\\test\\";
Avoid the backslashes, try using single quotes instead.
> unless (opendir(DIR, $articleDirName)) {
> warn "Can't open $articleDirName\n";
> closedir(DIR);
> exit(1);
> }
I like the fact you are checking the result of opendir, but
the action to take is flawed. If the directory open failed,
what are you closing?
Remember that die() is available, if you want to warn and
exit. Makes for shorter code, but you don't get to choose
the error number (see "perldoc -f die" for what you do get).
> foreach (readdir(DIR))
> {
> next if $_ eq '.' || $_ eq '..';
> $path = "$articleDirName$_";
> if (-f $path){
> if ($path =~ /\.fin/i){
> $doc = Win32::OLE->GetObject($path);
> $fileName = $path . ".rtf";
Consider using "$path.rtf" or "${path}.rtf", although that is just my
perference.
> print "$fileName\n";
> $doc->ActiveDocument->SaveAs({FileName => $fileName,
> FileFormat => wdFormatRTF})
'Can't call method "SaveAs" on an undefined value'.
That clearly means that "$doc->ActiveDocument" evaluates to be
undefined. I think you need: "$doc->ActiveDocument()->SaveAs",
those brackets telling perl it should be calling SaveAs on the value
returned by the ActiveDocument() method.
This might have been caught better using strict. Try.
> }
> undef $doc;
> undef $path;
> undef $fileName;
If you had declared your variables as lexically scoped, using my,
then you wouldn't have to undef them. With strict you will HAVE
to learn to use "my" and "our".
> }
> }
>
So, the main lesson of the day is USE STRICT. You will save
days of debugging, which must qualify as being kind (if not gentle ;-)
Jonathan Paton
--
#!perl
$J=' 'x25 ;for (qq< 1+10 9+14 5-10 50-9 7+13 2-18 6+13
17+6 02+1 2-10 00+4 00+8 3-13 3+12 01-5 2-10 01+1 03+4
00+4 00+8 1-21 01+1 00+5 01-7 >=~/ \S\S \S\S /gx) {m/(
\d+) (.+) /x,, vec$ J,$p +=$2 ,8,= $c+= +$1} warn $J,,
Post Follow-up to this messageFrom: Jonathan Paton <jepaton@gmail.com>
>
> 'Can't call method "SaveAs" on an undefined value'.
>
> That clearly means that "$doc->ActiveDocument" evaluates to be
> undefined. I think you need: "$doc->ActiveDocument()->SaveAs",
> those brackets telling perl it should be calling SaveAs on the value
> returned by the ActiveDocument() method.
I think ActiveDocument is not a method, but a property. So the right
syntax would be
$doc->{ActiveDocument}->SaveAs(...
Mark, you may need to play with the syntax a bit, Perl is in these
parts more strict than VB(Script) and you do have to distunguish
between methods and properties in Perl even on places where you could
treat them the same in VB(Script).
It may be helpful to use the OLE Viewer installed with ActivePerl and
see what properties and methods do the object have.
Jenda
===== Jenda@Krynicky.cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery
Post Follow-up to this messageJonathan and Jenda, I appreciate you taking the time to run through the basics. I'll make sure to use strict and warnings from now on. I sure hope it saves the time you are suggesting. Sometimes the debugging drives me nuts. The difference between properties and methods is difficult to determine...Microsoft's documentation isn't particularly user friendly. But with the help of kind strangers I'm muddling through. Thanks again.
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.