Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

How do I obtain a list of values for all the properties of a Word document using Win32::OLE?

by romandas (Pilgrim)
on Jun 11, 2009 at 18:02 UTC ( [id://770721]=perlquestion: print w/replies, xml ) Need Help??

romandas has asked for the wisdom of the Perl Monks concerning the following question:

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?

  • Comment on How do I obtain a list of values for all the properties of a Word document using Win32::OLE?
  • Select or Download Code

Replies are listed 'Best First'.
Re: How do I obtain a list of values for all the properties of a Word document using Win32::OLE?
by ikegami (Patriarch) on Jun 11, 2009 at 18:26 UTC

    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

    When you brought up your problem in the CB, I postulated that the reason you were running our of memory is that properties formed an infinite loop. You've just confirmed this.

    The problem isn't in Data::Dumper. It detects loops in data.

    $ perl -MData::Dumper -e'my $x = {}; $x->{x} = \$x; print Dumper $x' $VAR1 = { 'x' => \$VAR1 };

    The problem is that Win32::OLE doesn't do such checks. When it receives an object, it create a new Perl object to serve as a bridge. Data::Dumper correctly thinks this is a different object, so it proceeds to dig deeper.

      I understand what you're saying, but I would appreciate some clarification on a couple things...
      You mentioned that Win32::OLE doesn't perform the checks, but that Data::Dumper does. However, if I understand my own code (unlikely, but I have hope), I'm not calling Win32::OLE repeatedly.. I thought I just had one Data::Dumper call, and it took care of the rest.. thus, if it functioned as you say, wouldn't it find the 'infinite' loop?

      I suspect I'm not understanding how Data::Dumper works. BTW, above I'm referring to when I just passed $doc straight to Data::Dumper, not when I used the recursive function. Does that make sense?

        Win32::OLE objects aren't copies of the foreign objects. If they were copies, you couldn't affect the remote objects. Each Win32::OLE object is an interface to a remote objects.

        Any fetch from the referenced hash results in a get from the remote object.

        Any change to the referenced hash results in a set in the remote object.

        And yes, you are doing repeated fetches.

        I suspect I'm not understanding how Data::Dumper works.

        It works much like your code, except it doesn't print anything. The output is placed in a string, which is returned when it's done.

Re: How do I obtain a list of values for all the properties of a Word document using Win32::OLE?
by Anonymous Monk on Jun 11, 2009 at 18:07 UTC

      Tried it; didn't work at all -- it just generates a stream of errors, without anything meaningful.

      I figured that would be the case, but wanted to be fair and try your suggestion.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://770721]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2024-04-25 20:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found