Correlate Range.Text to Range.Start and Range.End

依然范特西╮ 提交于 2019-12-23 17:52:56

问题


I'm using regular expressions to search against the plain text returned by the following property:

namespace Microsoft.Office.Interop.Word
{
    public class Range
    {
        ...
        public string Text { get; set; }
        ...
    }
}

Based upon the matches I want to make changes to the formatted text that corresponds to the plain text. The problem I have is that the indices of characters in the .Text property do not match up with the .Start and .End properties of the Range object. Does anyone know any way to match these indices up?

(I can't use the Word wildcard find capabilities (as a replacement for .NET regular expressions) because they aren't powerful enough for the patterns I'm searching (non-greedy operators etc.))

I can move the correct number of characters by starting with Document.Range().Collapse(WdCollapseStart)and then range.MoveStart(WdUnitChar, match.Index) since moving by characters matches the formatted text position up with the matches in the plain text.

My problem now is that I'm always 4 characters too far along in the formatted text...so maybe it has something to do with the other story ranges? I'm not sure...


回答1:


Apparently the reason my matches were still off had to do with hidden "Bell" characters (char bell = '\a';). By replacing these with the empty string inside Application.ActiveDocument.Range().Text, my matches on this property now match up correctly with the range achieved by:

Word.Range range = activeDocument.Range();
range.Collapse(Word.WdCollapseStart);
range.MoveStart(Word.WdUnits.Character, regexMatch.Index);

Basically you can mirror indexes in the .Text property by moving through the formatted text character-by-character. The only caveat is that you need to remove the strange characters such as the bell character from the .Text property.



来源:https://stackoverflow.com/questions/3772938/correlate-range-text-to-range-start-and-range-end

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!