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('<IMG border=0 hspace=0 src="%s">', [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('<IMG border=0 hspace=0 src="%s">', [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('<IMG border=0 hspace=0 src="%s">', [urlStr]);
     ovTextRange.pasteHTML(tmpStr);  //粘贴图片源码
   end;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值