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('<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;  

相关阅读 >>

Delphi 利用vclzip实现分卷压缩

Delphi 最全_日期格式_dateutils时间单元说明

Delphi 将base64字符串转化为jpeg图片

Delphi禁止tedit, tmemo右键上下文弹出菜单

Delphi deletefile 删除文件

Delphi tfilestream 流操作2

Delphi 如何判断clipboard剪切板中的内容的类型

Delphi fmx自定义对话框样式,多屏幕布局

Delphi ado通用操作数据单元

Delphi如何获取qq2010聊天窗口句柄

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...