2017-10-13 4 views
0

データベースに格納されている画像をタイプフィールドと共に取得しました。その画像をAまたはBと言います。これらの画像を単語テンプレート、imgPlaceholder1という名前のフィールドに追加しようとしていますイメージの種類に応じてimgPlaceholder2を指定します。私の現在のコード:単語の複数の画像を追加するdoc

 $wordTable = new COM("Word.Application") or die("Unable to instanciate Word"); 
$wordTable->visible = 1; 
$wordTable->Documents->Open("c:\template\file.doc"); 

    try { 
     $query = "select id,path,type from uploads Where id = '$Id'"; 
     $action = mysql_query($query) or die(mysql_error()); 
      while($result = mysql_fetch_array($action)){ 
       $type=$result['type']; 
       $path=$result['path']; 
       if($type=='A'){ 
        $wordTable->Selection->Find->ClearFormatting(); 
        $wordTable->Selection->Find->Text = 'imgPlaceholder1'; 
        $wordTable->Selection->Find->Execute(); 
        $wordTable->Selection->InlineShapes->AddPicture($path,False,True); 
       } 
       if($type=='B'){ 
        $wordTable->Selection->Find->ClearFormatting(); 
        $wordTable->Selection->Find->Text = 'imgPlaceholder2'; 
        $wordTable->Selection->Find->Execute(); 
        $wordTable->Selection->InlineShapes->AddPicture($path,False,True); 
       } 
      } 
    } 
    catch (Exception $e) 
     { 
      echo $e->getMessage(); 
     } 

今、コードは動作しますが、何か変です型を無視し、すべてを最初のプレースホルダに引っ張ります。私は選択 - >検索 - >テキストのためにそれを信じていますが、私は選択をクリアするか、何か新しいものを検索するように強制する方法は知られていません。 また、ドキュメントを保存して閉じてから、2番目のタイプのイメージを挿入しようとしましたが、結果は同じです。すべての画像が最初のプレースホルダーになります。

答えて

0

自分で解決しました。 これら3行は非常に遠く、互いにことができない、または彼らは悲しい取得し、動作を停止:

$wordTable->Selection->Find->ClearFormatting(); 
$wordTable->Selection->Find->Text = '_fl_ImgTest_1'); 
$wordTable->Selection->Find->Execute(); 

、あなたは、いくつかのコーディングの自由を取得し、最終的に追加することができます。

$wordTable->Selection->InlineShapes->AddPicture($path,False,True); 

繰り返しと手続き2番目の文字列を見つけると、それは動作します

関連する問題