的范围不能被删除。在Microsoft.Office.Interop.Word.Range.set_Text(字符串道具) [英] The range cannot be deleted. at Microsoft.Office.Interop.Word.Range.set_Text(String prop)

查看:946
本文介绍了的范围不能被删除。在Microsoft.Office.Interop.Word.Range.set_Text(字符串道具)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与文本显示非常简单,我已经看到了同样的代码遍布在这么多的网站网(包括你的,从2009年9月后),但推荐的C#.NET代码替换书签,我不能得到过去的错误




范围无法删除。在
Microsoft.Office.Interop.Word.Range.set_Text(字符串
道具)




(I'中号使用VS 2010与Windows 7和Word 2010中14.0)。



我的代码:

 私人无效ReplaceBookmarkText(Microsoft.Office。 Interop.Word.Document DOC,串bookmarkName,字符串文本)
{

{
如果(doc.Bookmarks.Exists(bookmarkName))
{
对象名称= bookmarkName;
//抛出错误'的范围不能被删除'
doc.Bookmarks.get_Item(参考名称).Range.Text =文本;
}
}


解决方案

相反直接更改范围,尝试这样的:

 收藏书签= doc.Bookmarks.get_Item(REF名); 

//选择文本。
bookmark.Select();

//覆盖的选择。
wordApp.Selection.TypeText(文本);



例如。使用您的Word应用程序实例来改变文档而不是。


The recommended c# .net code to replace a bookmark with text appears very straight forward and I have seen the same code all over the net on so many websites (including yours, from a Sept. 2009 post) however, I cannot get past the error

The range cannot be deleted. at Microsoft.Office.Interop.Word.Range.set_Text(String prop)

(I'm using VS 2010 with Windows 7 and Word 2010 14.0).

My code:

 private void ReplaceBookmarkText(Microsoft.Office.Interop.Word.Document doc, string bookmarkName, string text)
        {
            try
            {
                if (doc.Bookmarks.Exists(bookmarkName))
                {
                    Object name = bookmarkName;
                    //  throws error 'the range cannot be deleted' 
                    doc.Bookmarks.get_Item(ref name).Range.Text = text;
                }
            }

解决方案

Instead of altering the range directly, try something like:

Bookmark bookmark = doc.Bookmarks.get_Item(ref name);

//Select the text.
bookmark.Select();

//Overwrite the selection.
wordApp.Selection.TypeText(text);

E.g. use your Word application instance to alter the document instead.

这篇关于的范围不能被删除。在Microsoft.Office.Interop.Word.Range.set_Text(字符串道具)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆