替换书签内容而不删除书签 [英] Replace bookmarks content without removing the bookmark
本文介绍了替换书签内容而不删除书签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要替换书签的文本内容而不丢失书签.
I want to replace the text content of bookmarks without loosing the bookmark.
foreach(Bookmark b in document.Bookmarks)
{
b.Range.Text = "newtext"; // text is set in document but bookmark is gone
}
我尝试在文本"设置之前设置书签的新范围",但是仍然存在相同的问题.
I tried to set the new Range of the bookmark before the Text setting but I still have the same problem.
我还尝试使用document.Bookmarks.Add(name, range);
重新添加书签,但是我无法创建range的实例.
I also tried to re-add the bookmark with document.Bookmarks.Add(name, range);
but I can't create an instance of range.
推荐答案
我必须阅读书签并临时保存范围.我还必须添加一个已处理项目的列表,以避开一个无限循环.
I had to readd the bookmarks and save the range temporarily. I also had to add a list of processed items to evade an endless loop.
List<string> bookmarksProcessed = new List<string>();
foreach (Bookmark b in document.Bookmarks)
{
if (!bookmarksProcessed.Contains(b.Name))
{
string text = getTextFromBookmarkName(b.Name);
var newend = b.Range.Start + text.Length;
var name = b.Name;
Range rng = b.Range;
b.Range.Text = text;
rng.End = newend;
document.Bookmarks.Add(name, rng);
bookmarksProcessed.Add(name);
}
}
这篇关于替换书签内容而不删除书签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文