I am trying to modify the built in document properties for a word document based on input from a PHP form.

I have been able to open the document and get the value, but I have not been able to set the value. Here is my code.

// starting word
$word = new COM("word.application") or die("Unable to instanciate Word");
// echo "Loaded Word<br>";//, version {$word->Version}\n";

//bring it to front
$word->Visible = 1;

//open a document
$word->documents->Open($upfile) or die("Couldn't Open document");
$doc = $word->documents[1] or die("Couldn't reference document");

// Get Metadata
$strTitle = $doc->BuiltInDocumentProperties[1]->Value;
$strSubject = $doc->BuiltInDocumentProperties[2]->Value;
$strAuthor = $doc->BuiltInDocumentProperties[7]->Value;
$strKeywords = $doc->BuiltInDocumentProperties[4]->Value;

	 // Niether of the below code sets returns an error
	 // Niether changes the property value
	 // Apparently, PHP has overloaded the = sign 

	 //Set 1
	 //Properties dialog 
     $dp = $word->Dialogs[86];
     // Set "Title" to a new value.
     $dp->Title = "My Title";
     // Set the value without showing the dialog.
     $dp->Execute;

	  //Set 2
     $dp = $doc->BuiltInDocumentProperties;
     //' Set KeyWords for the active document by
     //' using a literal string.
	 $kw= $dp[7] ;	
     $kw->Value = "Using Builtindocumentproperties";

$doc->Save();
//closing word
$doc->Close();
$word->Quit();

//free the objects

$doc = null;
$word = null;

Any help would be appreciated

    3 months later

    This is how I set the property within Powepoint:

    $ppoint = new COM("PowerPoint.application") or die("Unable to instantiate PowerPoint");
    $ppoint->Presentations->Add() or die ("Could not create presentation");

    ...

    //Add a slide to the document
    $secondslide=$ppoint->Presentations[1]->Slides->Add(1,1);
    $slidename2 = $secondslide->Name();
    echo"<br>slidename 2: $slidename2";

    //Set the name of this new slide
    $secondslide->Name = "New Slidename";
    $slidename2 = $secondslide->Name();
    echo"<br>NEW slidename 2: $slidename2";

    Watch the brackets :
    Name <--> Name()

    It might help you - good luck !

      2 months later

      I have been doing the same thing in a VB application and have found some quirks. The Title appears to be the hardest thing to set as I have not been able to do so. The Dialogs(86) which is the SummaryItems dialog is not availabe unless the documents active window is 'Active'. Because you are accessing it through automation, I don't think it will be set active. At least this was true in my case. The Keywords should be a different matter though. you might try changing the line the lines:

      $kw= $dp[7] ;
      $kw->Value = "Using Builtindocumentproperties";

      To:

      $dp[7]->Value = "Using Builtindocumentproperties";

      I'm not sure if that will help as I haven't tried this in PHP but I hope it does.

      Hope that helps,

      Ed Evans

        6 years later

        Hi did you found a solution to your Problem?
        I have just started a similar project and have the same problems now!

        Thanks in advance!
        Soenke

          Write a Reply...