php 使用 COM 替换 msword 中的文本

php replace text in msword using COM

我正在尝试使用 php 替换 msword 文档中的文本。使用 COM。 抛出错误 [0x80020011] 不支持集合。 代码是从 VBA.

复制和修改的
       $word = new COM("Word.Application") or die("Word not installed!");
        try{
            $word->Documents->Open("c:.doc");
            $word->ActiveDocument->Range->Find->Text="222";
            $word->ActiveDocument->Range->Find->Replacement->Text="111";
            $word->ActiveDocument->Range->Find->Forward=True;
            //$word->ActiveDocument->Range->Find->Replace=2;
            //$word->ActiveDocument->Range->Find->Wrap=1;
            $word->ActiveDocument->Range->Find->Format=False;
            $word->ActiveDocument->Range->Find->MatchCase=False;
            $word->ActiveDocument->Range->Find->MatchWholeWord=False;
            $word->ActiveDocument->Range->Find->MatchWildcards=False;
            $word->ActiveDocument->Range->Find->MatchSoundsLike=False;
            $word->ActiveDocument->Range->Find->MatchAllWordForms=False;
            $word->ActiveDocument->Range->Find->Execute();
            $word->ActiveDocument->SaveAs("c:.doc");
            $word->Quit();
       }
       .....

另一种方式

   $wdFindContinue = 1;
   $wdReplaceAll = 2;
   $word->ActiveDocument->Content->Find->Execute("222", false, false, false, false, false, true, $wdFindContinue, false, "111", $wdReplaceAll);

服务器正在使用 office 2007,并且 php 4.6 /apache 2.1

更新:

不要在php中使用它,它不会替换所有字符串。

另一种解法:

  1. 将单词另存为xml
  2. 然后使用php fread将其作为文本打开
  3. 替换里面的字符串.

并保存到文件。