Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing Office file Properties

Status
Not open for further replies.

ivpotter

Programmer
Jan 19, 2001
13
0
0
GB
Hi

I am trying to extract dome properties from a MS Word document using the following code ...

use Win32::OLE;

$doc = Win32::OLE->new('DSOleFile.PropertyReader', 'Quit') || die "\"Unable to create PropertyReader\"";
$doc_prop = $doc->GetDocumentProperties("./a.doc") || die "\"Unable to process properties of file\"";

print $doc_prop->author;
print $doc_prop->title;
print $doc_prop->comments;

... however the code fails to create the document object.

Can anyone see what I am doing wrong please.

Many thanks

Ivor
 
Ivor,

What is the value of $! directly after the failure? Mike

"Experience is the comb that Nature gives us, after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 
Mike

Thanks for your answer but I've now solved it - I spent ages trawling the net trying to find the answer to this thinking it a perl problem.

Since posting I tried achieving the same via VB and discovered that I didn't have DSOleFile installed - I'd presumed that this would be available if I had Win32::OLE available and I had already proved that to be the case.

Thanks anyway

Ivor
 
You could achieve this by accessing the datecreated method of $doc_prop via my original code.

You may need to install DSOleFile which can be obtained from ...


Unfortunately I'm not sure how to interpret the format of the value returned and I'm struggling to find any info on this.

Hope you have better luck.

Cheers

Ivor
 
Glad you're sorted Ivor Mike

"Experience is the comb that Nature gives us, after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 

Excellent people using dsofile in perl for document properties. I can read them also but I can set them!

My script is a little messy (no perl expert but I love it). It will not set the properties am I doing something stupid? Thanks. BTW I do have a tool that does this in VB but it resets the dates and perl is simpler to use so I'd rather learn more perl. When I can write the properties I can set the modified dates back at the end of the job.


# use strict;
use Win32::OLE;
use File::Spec;

my $PropertyReader = Win32::OLE->new('DSOleFile.PropertyReader', 'Quit');

my $directory = "c:\\temp\\cst";


opendir(DNAME, $directory) || die "Unable to open the requested directory: $directory\n";

while( my $filename = readdir( DNAME ) ) {

next if ($filename eq '.' or $filename eq '..');
# next if /\.doc$/;

my $fullfilename = File::Spec->catdir($directory,$filename);

my $properties = $PropertyReader->GetDocumentProperties($fullfilename)

# my $propertiez = $PropertyReader->SetDocumentProperties($fullfilename)

|| die("Unable read file properties for '$fullfilename' ", Win32::OLE->LastError());


if (0 == 0)
# if ( !$properties->{title} || length($properties->{title}) == 0)
{
# print "File '$filename' --- Title not set setting to this\n";

$properties->SetProperty('title', "this one");

$properties->SetProperty('author', "this");
$properties->SetProperty('comments', "this");
$properties->SetProperty('company', "this");
$properties->SetProperty(title, "this one");
$properties->SetProperty(Title, "this one");
$properties->SetProperty({title}, "this one");
$properties->SetProperty("title", "this one");



# print "me File '$filename' --- Title property is set to '" . $properties->{title} ."'\n";

# print $properties->author;
print $properties->title;
print "\n\n";

# print $properties->comments;
# print $properties->datecreated;
# print $properties->company;


# } else {
# print "File '$filename' --- Title property is set to '" . $properties->{title} ."'\n";

}

}

closedir(DNAME);
 
If anyone gets here looking for document properties settings this script works really well. Get the latest dsofile.exe from MS first and try this.

use File::Find;
use Win32::OLE;

$feeddir = @ARGV[0];
$feeddir =~ s/\\/\//; # If the dir is given using \ (like c:\temp) then subsitute all \ for /'s
$feeddir =~ s/\\/\//;
$feeddir =~ s/\\/\//;
#if ($feeddir eq "")
#{
#system("cls");
#print "$feeddir\n";
#print "\n\n\n\nNo directory specified\n\n";
#print "You need to specify a driectory to search down EG... thisfile.pl c:\\csvdir\n\n\n\n\n";
#die;
#}

chdir("$feeddir"); # Change working directory to the now corrected directory given on the command line

find(\&wanted, "$feeddir"); # use the built in find command with the wanted subroutine and send it the $feeddir variable


sub wanted # the wanted subrouting is used to select only documents and change the company name

{
# pass over any file not ending in doc/DOC/xls/XLS etc

/\.doc$/ or /\.DOC$/ or /\.xls$/ or /\.XLS$/ or /\.ppt$/ or /\.PPT$/ or /\.dot$/ or /\.DOT$/ or return;
$filename = $File::Find::name;


my $PropertyReader = Win32::OLE->new('DSOleFile.PropertyReader', 'Quit');
my $comp = "see you in hell";
#print "$filename\n";
my $properties = $PropertyReader->GetDocumentProperties($filename);
# || die("Unable read file properties for '$fullfilename' ", Win32::OLE->LastError());

if ( !$properties->{title} || length($properties->{title}) >= 0)
{

# heart of the file Record filename / current company field / current date
# change the company name and print file name again and company name


print "1 $filename CN $properties->{'company'} \n";
$properties->{'company'} = $comp; #|| die("Unable set 'Title' for '$filename' ", Win32::OLE->LastError());
print "2 $filename CN $properties->{'company'} \n\n";


}
}
closedir(DNAME);








 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top