2017-10-13 110 views
0

我已经将几个图像与类型字段一起存储在数据库中,可以说它是A或B.我试图将它们添加到单词模板中,放入我命名为imgPlaceholder1的字段中和imgPlaceholder2取决于图像的类型。继承人我目前的代码:在word中添加多个图像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(); 
     } 

现在,代码工作,但它确实有点奇怪。它会忽略该类型并将所有内容拉入第一个占位符。我相信它是因为选择 - >查找 - >文本,但我不知道如何清除选择或迫使它搜索新的东西。 我也尝试保存并关闭文档,然后插入第二种类型的图像,但结果是相同的。所有图像都以第一个占位符结束。

回答

0

自己解决。 这3条线不能很远的对方,或者他们找悲伤而停止工作:

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

那么你可以得到一些编码的自由,最后加:

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

重复这个过程与第二个字符串找到,它会工作