Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2024-03-29 14:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found