[PHP-users 31946] COMでの文字化け

Satto satoru_kumagai @ mjs.co.jp
2007年 4月 17日 (火) 09:33:32 JST


COMを利用し,Wordの表内に文字列をセットする仕組みを
考えています.

環境
SV -> Windows2000
Apache -> 1.3.26
PHP -> 4.3.9

UTF-8で記述しています.
<?php
class setWord {
 var $Word;
 var $doc;
 var $fname;
 
 function setWord(){
  
  $this->Word = new COM('Word.Application');
  $this->Word -> Visible = false;
  $this->Word -> DisplayAlerts = 0;
 }
 
 function documentOpen(){
  $this->doc = $this->Word->Documents->Open('d:/temp/hoge.doc');
  $this->doc -> Activate;
 }
 
 function setParam($param){
  $tbl = $this -> doc->Tables(1);
  $cell = $tbl -> Cell(1,1);
  $cell -> Range -> Text = mb_convert_encoding($param, 'SJIS', 'UTF-8');
 }
 
 function SaveAs(){
  mt_srand();
  $this->fname = mt_rand(1000000000, 2147483647);
  $this->Word -> Documents[1] -> SaveAs("d:/temp/{$this->fname}.doc");
 }
 
 function unsetObj(){
  $this->Word -> Documents[1] -> Close();
  $this->Word -> Quit();
  $this->Word = null;
 }
 
 function returnWord($param){
  $this->documentOpen();
  $this->setParam($param);
  $this->SaveAs();
  $this->unsetObj();
 }
}

$cls = new setWord();
$cls -> returnWord('日本語');
?>

hoge.docをオープンし,表に文字列をセット.それを別名で保存.
結果,正しく動作しファイルが生成されるのですが,そのファイルを
開くと,セットされた文字列「日本語」の後ろに「却」のような文字が
化けてついてしまいます.
日本語却却却 のような感じです.
「あいうえお」の場合は,あいうえお却却却却却 となります.

文字数分だけ化けた文字がついてしまう原因を調査しているのですが,
解決できません.

ご教授,お願いします.


PHP-users メーリングリストの案内