您的位置:首页 > 移动开发 > Objective-C

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

2017-04-11 09:37 726 查看
先看下面的源码,在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 TWebBrowser
相关文章推荐