gpt4 book ai didi

c# - 范围不能删除。在 Microsoft.Office.Interop.Word.Range.set_Text(字符串属性)

转载 作者:太空狗 更新时间:2023-10-29 17:47:14 26 4
gpt4 key购买 nike

推荐的用文本替换书签的 c# .net 代码看起来非常简单,我在网上看到了很多网站(包括你的网站,来自 2009 年 9 月的一篇帖子)上的相同代码,但是,我无法获得过去的错误

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

(我在 Windows 7 和 Word 2010 14.0 中使用 VS 2010)。

我的代码:

 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;
}
}

最佳答案

与其直接改变范围,不如尝试这样的事情:

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

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

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

例如使用您的 Word 应用程序实例来更改文档。

关于c# - 范围不能删除。在 Microsoft.Office.Interop.Word.Range.set_Text(字符串属性),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4962712/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com