perlquestion
romandas
<p>This might ramble but bear with me.</p>
<p>Ultimately, I am trying to automate some Word (2003) document editing for our shop using ActiveState Perl 5.10 and Win32::OLE. I haven't done any OLE programming before, so it's slow going. I did read through the excellent [id://153486|tutorial] as well as the CPAN Win32::OLE examples (and used some of the example code), but don't see an answer to my question there.</p>
<p>I successfully created a OLE server object and opened the test file (Word document with 'TEST' as the only content). I noticed that my $doc variable is shown as a Win32::OLE=HASH, so I tried to enumerate all the properties (keys) and their values, which is where I ran into trouble. Here's my code so far:</p>
<code>#!/perl/bin/perl
use warnings;
use strict;
use Win32::OLE;
use Win32::OLE::Const;
use Data::Dumper;
Win32::OLE->Option(Warn => 2);
my $wordConsts = Win32::OLE::Const->Load("Microsoft Word 11.0 Object Library");
# use existing instance if Word is already running
my $server;
eval {$server = Win32::OLE->GetActiveObject('Word.Application')};
die "Word not installed" if $@;
unless (defined $server) {
$server = Win32::OLE->new('Word.Application', sub {$_[0]->Quit;})
or die "Oops, cannot start Word";
}
my $doc = $server->Documents->Open('D:\perlstuff\testDocument.doc');
# $doc->SaveAs('d:\testSave.txt', $wordConsts->{'wdFormatDOSText'});
parseProps($doc);
sub parseProps {
my %hash = %{$_[0]};
foreach my $key (sort(keys %hash)) {
print("$key = ");
if (!(UNIVERSAL::isa($hash{$key}, "HASH"))) {
print("not a hash.\n");
} else {
print("HASH ref\n");
parseProps($hash{$key});
}
}
}</code>
<p>I originally tried to pass $doc to Data::Dumper, but that died with an 'Out of Memory' error. I think that may be related to why my enumeration isn't working any other way I've tried, but I haven't nailed down the root cause.</p>
<p>I then wrote the recursive function you see in my code above, which seems to start enumerating.. but there are several problems with it.</p>
<p>First, it seems to be not just listing values, but executing calls somehow -- as it runs, I get a pop-up to set up Outlook profiles (Outlook isn't setup; this is a development machine), which I have to cancel for it to continue running.</p>
<p>Second, there seems to be a property whose value itself recurses, because I keep getting the same properties over and over after a short period of time. Here is a list of the properties I've seen:</p>
<code>Compatibility
PrintFractionalWidths
Container
ActiveWritingStyle
HasMailer
Mailer
VBProject
Frameset
MailEnvelope
DisableFeaturesIntroducedAfter
Permission
DocumentLibraryVersions
Frameset
International
MacroContainer
SynonymInfo
VBE
KeysBoundTo
FindKey
IsObjectValid
FileDialog
Compatibility</code>
<p>Any ideas where I've gone wrong?</p>