Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Win32::OLE, Word and CustomDocumentProperties

241 views
Skip to first unread message

Tiernan Mc Guigan

unread,
Sep 29, 1998, 3:00:00 AM9/29/98
to
Firstly I've done a search for Win32::OLE and Word on DejaNews to no avail.

I'm trying to modify a Word document's custom document properties using
Perl and Win32::OLE. Unfortunately I haven't been very successful. No
matter what I try, I can't get at these properties never mind set them.

Here's my code --

use Win32::OLE;

my $DocumentName = "C:\\WINDOWS\\DESKTOP\\pDocGen\\RIS.doc";

### Get Word running.

print "\nGetting Word ready\n";

my $MSWord;

# Use existing instance if Word is already running.
eval { $MSWord = Win32::OLE->GetActiveObject('Word.Application') };
die " MS Word not installed" if $@;

if (! defined $MSWord) {
$MSWord = Win32::OLE->new('Word.Application', sub {$_[0]->Quit;})
|| die "Cannot start MS Word\n";
print " Launched Word.\n\n";
} else {
print " Word already running.\n\n";
}

### Open the document.

print "Opening $DocumentName\n";

my($Document) = $MSWord->Documents->Open( $DocumentName );

my($DocumentProperties) = $Document->CustomDocumentProperties;

print "$DocumentProperties\n";

foreach $Key ( keys %{ $DocumentProperties } ) {
print "Property:", $Key,
" Value:", $%{ $DocumentProperties }{$Key};
}

And here's the output --

Getting Word ready
Word already running.

Opening C:\WINDOWS\DESKTOP\pDocGen\RIS.doc
Win32::OLE=HASH(0x658e54)

To me it looks like I got an empty hash back from
$Document->CustomDocumentProperties.

Has anybody any ideas?

-------------------------------
EMail: tpjmc...@hotmail.com

Jan Dubois

unread,
Sep 29, 1998, 3:00:00 AM9/29/98
to
[mailed & posted]

"Tiernan Mc Guigan" <tmcg...@bfsec.bt.co.uk> wrote:

>Firstly I've done a search for Win32::OLE and Word on DejaNews to no avail.
>
>I'm trying to modify a Word document's custom document properties using
>Perl and Win32::OLE. Unfortunately I haven't been very successful. No
>matter what I try, I can't get at these properties never mind set them.

My documents don't have custom properties, but here is some code to display
the builtin properties. I hope this gives you the right ideas about the
DocumentProperties collection object (containing DocumentProperty objects).

-Jan

use strict;
use Win32::OLE qw(in);

my $Word = Win32::OLE->new('Word.Application', 'Quit');
my $Doc = $Word->Documents->Open('i:\tmp\test.doc');

foreach my $Property (in $Doc->BuiltinDocumentProperties) {
my $Name = $Property->Name;
local $Win32::OLE::Warn = 0;
my $Value = $Property->Value;

$Value = '***Error***' if Win32::OLE->LastError;
$Value = '<undef>' unless defined $Value;
printf "%s %s = %s\n", $Name, '.' x (40-length($Name)), $Value;
}
$Doc->{Saved} = 1;


Produces for me:

Title ................................... = Sample Test
Subject ................................. =
Author .................................. = Jan Dubois
Keywords ................................ =
Comments ................................ = <undef>
Template ................................ = Normal.dot
Last author ............................. = Jan Dubois
Revision number ......................... = 5
Application name ........................ = Microsoft Word 8.0
Last print date ......................... = ***Error***
Creation date ........................... = 15.06.98 22:31:00
Last save time .......................... = 29.09.98 20:49:00
Total editing time ...................... = 0
Number of pages ......................... = 1
Number of words ......................... = 10
Number of characters .................... = 44
Security ................................ = 0
Category ................................ =
Format .................................. = <undef>
Manager ................................. = <undef>
Company ................................. =
Number of bytes ......................... = 19456
Number of lines ......................... = 3
Number of paragraphs .................... = 3
Number of slides ........................ = ***Error***
Number of notes ......................... = ***Error***
Number of hidden Slides ................. = ***Error***
Number of multimedia clips .............. = ***Error***
Hyperlink base .......................... = <undef>
Number of characters (with spaces) ...... = 51


Tiernan Mc Guigan

unread,
Oct 1, 1998, 3:00:00 AM10/1/98
to
I now have what I wanted, and am posting the final code here so others
don't re-invent the wheel. Thanks to Jan Dubois for pointing me in the
right direction.

use strict;
use integer;
use Win32::OLE qw(in);

my $DocName = 'C:\DOCS\myDoc.doc';

my(%FieldValues) = (

"Custom_Title" => "An example document title",
"Custom_Author" => "Tiernan Mc Guigan",
"Custom_Date" => "01/01/99",

);

### Re-use any instances of Word, or start a new one.

print "\n",
"Attempting to use an existing instance of Word...\n";

my $Word = Win32::OLE->GetActiveObject('Word.Application');

if(not defined $Word) {
print " No existing instances; creating one", "\n\n";


$Word = Win32::OLE->new('Word.Application', 'Quit');

} else {
print " Using an existing instance.", "\n\n";
}

### Open the document.

print "Opening $DocName...\n";

my $Doc = $Word->Documents->Open($DocName);

if(not defined $Doc) {
print " Unable to open $DocName.",
"\n\n";
} else {
print " $DocName opened",
"\n\n";
}

### Update the custom document property values.

foreach my $Property (in $Doc->CustomDocumentProperties) {
if ( defined $FieldValues{$Property->Name} ) {
print "Property \"", $Property->Name, "\" currently set to \"",
$Property->Value, "\"",
"\n",
"Changing it to \"", $FieldValues{$Property->Name}, "\"",
"\n\n";
$Property->{Value} = $FieldValues{$Property->Name};
}
}
...

T.

0 new messages