tags:

views:

395

answers:

1

I'm trying to manipulate some fields in a supplied Word 2003 document using the document's own bookmarks using PHP and COM but am getting an error which ever method that I use. If I try to call the Bookmarks directly to substitute the text I get an error: The range cannot be deleted.

 function testBkMrkDetails($word, $bookmarkName, $subsTxt) {

try {
   $BkMark = $word->ActiveDocument->Bookmarks($bookmarkName);
   $range = $BkMark->Range;
   if (!$range) {
      echo "Range not created ";
   }
   $range->Text = $subsTxt;
} catch (Exception $e) {
 echo "bookmark failed: " . $e->getMessage() . "\n";
}
}

I've searched on Google using the error message and loking for PHP and COM tutorials which appeared to suggest that I look at FormFields so I wrote the following:

 function testFormFlds ($word, $bookmarkName, $subsTxt) {

try {
 $formField = $word->Selection->FormFields($bookmarkName);
 if (!$formField) {
  die("Form failed : " . $bookmarkName . " not found\n");
 }

 $formField->Result($subsTxt);
} catch (Exception $e){
 echo "FormField failed: " . $e->getMessage();
}

}

However this keeps telling me that the requested member of the collection does not exists so I'm assuming that I have not called the feild name correctly (it was taen from the document). I would be grateful for some pointers towards solving this and learning more about the underlying technology. Thanks, Iain

A: 

Are you trying to manipulate fields (like with a mail merge), form field (like checkboxes) or just bookmarkes by themselves?

Tom Winter
I was trying to manipulate the bookmarks like a mail merge. I have managed to do this using a macro in Word and then running it from PHP by sending arguments to the macro: $word->Application->Run("macroname","bookmark", "text);I did find a way of getting PHP to do it with something like: $word->Selection->Goto("wdGoToBookmark", "wdGoToAbsolute", "bookmark name"); $word->Selection->TypeText("text");but I keep getting a bad parameter error.
IainE