如何更改 Word.Range 文本而不丢失格式
任何人都知道如何更改 Word.Range 对象的文本但仍保留其格式? 例如,如果我有“this text”并将其更改为“that txt”,则 txt 仍将以粗体显示。
我正在寻找一种方法来更改范围的整个文本,而不仅仅是单个单词,因为我从独立的 API 获取新文本,我可以假设新文本和旧文本具有相同的编号的话。
这是我到目前为止得到的:
for (int i = 0; i < oldWords.Length; i++)
{
if (oldWords[i] == newWords[i])
continue;
object Replace = WdReplace.wdReplaceOne;
object FindText = oldWords[i];
object ReplaceWith = newWords[i];
var success = Sentence.Find.Execute(parameters stub);
}
但由于某种原因,它只在第一次执行中成功,因为范围选择保留在找到的单词上。
编辑:明白了,每次执行后,我都恢复了范围的原始结束位置。
谢谢。
Anyone knows how can I change the text of a Word.Range object but still keeping it's format?
For example if I have "this text" and I change it to "that txt", txt will still be in bold.
I'm looking for a way to change the whole text of the range, not just a single word, as I'm getting the new text from an independent API, I can assume that the new text and the old text have the same number of words.
This is what I got so far:
for (int i = 0; i < oldWords.Length; i++)
{
if (oldWords[i] == newWords[i])
continue;
object Replace = WdReplace.wdReplaceOne;
object FindText = oldWords[i];
object ReplaceWith = newWords[i];
var success = Sentence.Find.Execute(parameters stub);
}
But for some reason, it only succeeds in the first Execute, because the range selection remains on the found word.
Edit: got it, after each execute, I had restore the original end position of my Range.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法使用
Execute
方法更改文本的格式。你可以这样做:
也许这可以帮助你。
You can't use the
Execute
method to change the text with formatting.You can do it like:
Maybe this can help you.