最近用到了:range.Characters.First.Delete函数,发现一个BUG,在此记录:
代码解释如下:
/// <summary>Deletes the specified number of characters or words.</summary>
/// <param name="Unit">Optional Object. The unit by which the collapsed range or selection is to be deleted. Can be one of the following <see cref="T:Microsoft.Office.Interop.Word.WdUnits" /> constants: wdCharacter (default) or wdWord.</param>
/// <param name="Count">Optional Object. The number of units to be deleted. To delete units after the range or selection, collapse the range or selection and use a positive number. To delete units before the range or selection, collapse the range or selection and use a negative number.</param>
[DispId(127)]
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
int Delete([MarshalAs(UnmanagedType.Struct), In, Optional] ref object Unit, [MarshalAs(UnmanagedType.Struct), In, Optional] ref object Count);
从代码说明可以看出
range.Characters.First.Delete(WdUnits.wdCharacter,2)和range.Characters.First.Delete(2)应该是相同结果,但是实际执行时候发现,range.Characters.First.Delete(WdUnits.wdCharacter,2)中加入WdUnits.wdCharacter是有BUG的,特别是待删除文本中包含一个空格的时候,会不识别为字符。
我的测试word是2021。
在此记录一下,供大家点评!