delphi TWebBrowser出现 Method pasteHTML not supported by automation object 解决方法  
官方Delphi 学习QQ群: 682628230(三千人)
频道

delphi TWebBrowser出现 Method pasteHTML not supported by automation object 解决方法


先看下面的源码,在TWebBrowser当前编辑位置插入一个图片,是通过源码的方法插入的。

var urlStr : string;  
    ovSelection: OleVariant;  
    ovTextRange: OleVariant;  
    tmpStr : string;  
begin  
  urlStr := 'http://www.1and1-mail.com/imgv2/pic_1.jpg';  
       ovSelection := Edit.OleObject.Document.selection; //获得选择对象  
       ovTextRange := ovSelection.createRange; // create a TextRange from the current selection  
       tmpStr := Format('', [urlStr]);  
       ovTextRange.pasteHTML(tmpStr);  //粘贴图片源码  
end;  

上面代码中,如果编辑的时不选择任何内容,或者选择了一部分文字,可以正常插入图片,但是如果原来选择的是个图片,或者是其它的比如按钮,录入框,在调用ovTextRange.pasteHTML时会出现 Method pasteHTML not supported by automation object 错误。
解决方法1:
先清空选择对象,如下代码

ovSelection := Edit.OleObject.Document.selection; //获得选择对象  
ovSelection.Clear;  //先清空  
ovTextRange := ovSelection.createRange; // create a TextRange from the current selection  
tmpStr := Format('', [urlStr]);  
ovTextRange.pasteHTML(tmpStr);    

解决方法2:
判断选择的类型,代码如下

ovSelection := Edit.OleObject.Document.selection; //获得选择对象  
if SameText(ovSelection.type, 'Text') or SameText(ovSelection.type, 'None') then   //只有选择文本、或不选择的地方可以插入  
begin  
  ovTextRange := ovSelection.createRange; // create a TextRange from the current selection  
  tmpStr := Format('', [urlStr]);  
  ovTextRange.pasteHTML(tmpStr);  //粘贴图片源码  
end;  


推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.1350839138031 seconds