Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Adding MSWord custom properties in Win32::OLE

by thoglette (Scribe)
on Aug 30, 2006 at 06:45 UTC ( [id://570319]=perlquestion: print w/replies, xml ) Need Help??

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

I've got some code to update custom properties - in essence

$property = "FooBar"; $doc->CustomDocumentProperties($property)->{Value} = $text_value;
However, this warns Can't use an undefined value as a HASH reference at C:\Public\Projects\gold\update_msword.pl if the custom property does not already exist. The appropriate VBA code is...
'If customdocument property did not exist, create it If Err.Number <> 0 Then Set DocProps = ThisWorkbook.CustomDocumentProperties DocProps.Add Name:="FooBar", _ LinkToContent:=False, _ Type:=msoPropertyTypeString, _ Value:=str_content End If

Which leads the following questions under Win32::OLE

  1. Where is Err.Number ? And I'd like to avoid the warn, so how can I see what custom properties exist.
  2. $doc->CustomDocumentProperties and $doc->{CustomDocumentProperties} both return undef, so how does one create $doc_props?
  3. is msoPropertyTypeString a constant constant or yet another $$wd{'msoPropertyTypeString'}, (where $wd=Win32::OLE::Const->Load($MSWord);)
I realise that my lack of understanding of VBA is not helping. If there's a book worth purchasing, suggestions would be welcomed!



Update 2006-09-06

  1. The error code is Win32::OLE::LastError(); - See perldoc Win32::OLE
  2. but you need to Win32::OLE->Option(Warn=>0); to avoid having errors stop the debugger.
  3. A better solution is to pre-test-for-existance, rather than post-test-for-error. See my followup post



Butlerian Jihad now!

Replies are listed 'Best First'.
Re: Adding MSWord custom properties in Win32::OLE
by jdtoronto (Prior) on Aug 30, 2006 at 08:28 UTC
    I can't give you much detail, but in our snippets collection from 5 or 6 years ago (Perl 5.6 days?) I found this:
    use strict; use Win32::OLE qw(in); use Win32::OLE::Const 'Microsoft Office'; my $Doc = Win32::OLE->GetObject('g:\scratch\test.doc'); if ( ( 0 + Win32::OLE::LastError() ) != 0 ) { $Doc->CustomDocumentProperties->Add( { Name => 'A custom property', LinkToContent => 0, Type => msoPropertyTypeString, Value => "Whatever" } ); }
    At the time we were doing lots of MS stuff, fortunately those days are now long past, but maybe this will help.

    jdtoronto

      Thanks. A little fettling gets the job done. Win32::OLE makes a whole lot of function calls look like empty hash references. But an empty hash ref is not undefined and can be tested for.
      $docprops = $doc->CustomDocumentProperties(); unless (defined ($doc->CustomDocumentProperties($property))) { $docprops->Invoke('Add', { Name => $property, LinkToContent => 0, Type => 4, #msoPropertyTypeString #strictures to work around still Value => $value, }); } $doc->CustomDocumentProperties($property)->{Value} = $value;
      It looks a little weird in the debugger, as the fact that the value is set is hidden for a little while (value is ''). Suspect it's a debugger/tie/Windows interaction.

      Thoglette.

      --
      Butlerian Jihad now!
Re: Adding MSWord custom properties in Win32::OLE
by teun-arno (Acolyte) on Mar 05, 2014 at 14:27 UTC

    When saving msword document after adjusting the custom properties you MUST set the Saved flag : see http://support.microsoft.com/kb/195425/en-us

    without it it won't be saved.......

    use strict; use Win32::OLE qw(in); use Win32::OLE::Const 'Microsoft Office'; #my ( $doc ) = Win32::OLE->GetObject('D:/4nt8/perl-msword_dap/excel_da +p/Inrichting Arag-ftp.doc'); my ( $doc ) = Win32::OLE->GetObject('//vldfps1/algemeen/FM&O/Klanten/S +-t/TESTING/Dap Dossier/01. Dap/FBL.OPR.UX.138 WI Inloggen, shutdown e +n reboot van LPAR AIX systemen m.b.v. de HMC en PTY.doc'); my $property = 'Arno'; my $value = 'KranenDONK'; my $docprops = $doc->CustomDocumentProperties(); unless (defined ($doc->CustomDocumentProperties($property))) { $docprops->Invoke('Add', { Name => $property, LinkToContent => 0, Type => 4, #msoPropertyTypeString #strictures to work around still Value => $value, }); } $doc->CustomDocumentProperties($property)->{Value} = $value; $doc->{Saved} = 0; # SEE : http://support.microsoft.com/kb/195425 en + http://compgroups.net/comp.lang.perl.misc/how-read-ms-word-summary-p +roperties/477092 $doc->Save; $doc-> Close;

    Hope somebody will find this quicker then I did....

Log In?
Username:
Password:

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

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

    No recent polls found