This might ramble but bear with me.

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 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.

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:

#!/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 L +ibrary"); # 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}); } } }

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.

I then wrote the recursive function you see in my code above, which seems to start enumerating.. but there are several problems with it.

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.

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:

Compatibility PrintFractionalWidths Container ActiveWritingStyle HasMailer Mailer VBProject Frameset MailEnvelope DisableFeaturesIntroducedAfter Permission DocumentLibraryVersions Frameset International MacroContainer SynonymInfo VBE KeysBoundTo FindKey IsObjectValid FileDialog Compatibility

Any ideas where I've gone wrong?


In reply to How do I obtain a list of values for all the properties of a Word document using Win32::OLE? by romandas

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.