我有一些代码可以使用数据集中的值来查找和替换word文档中的字段。
Word.Document oWordDoc = new Word.Document();
foreach (Word.Field mergeField in oWordDoc.Fields)
{
mergeField.Select();
oWord.Selection.TypeText( stringValueFromDataSet );
}
在某些情况下,stringValueFromDataSet
为空,除了不插入任何内容外,我还想删除当前行。
知道我该怎么做吗?
答案 0 :(得分:5)
好的,这最终是非常容易的。
oWord.Selection.TypeBackspace();//remove the field
oWord.Selection.TypeBackspace();//remove the line
答案 1 :(得分:4)
显然,你的答案适合你的情况。但是在一般情况下(其中 使用退格键无法删除整行。可以使用以下内容:
private static object missing = System.Reflection.Missing.Value;
/// <summary>
/// Deletes the line in which there is a selection.
/// </summary>
/// <param name="application">Instance of Application object.</param>
private void DeleteCurrentSelectionLine(_Application application)
{
object wdLine = WdUnits.wdLine;
object wdCharacter = WdUnits.wdCharacter;
object wdExtend = WdMovementType.wdExtend;
object count = 1;
Selection selection = application.Selection;
selection.HomeKey(ref wdLine, ref missing);
selection.MoveDown(ref wdLine, ref count, ref wdExtend);
selection.Delete(ref wdCharacter, ref missing);
}