如何使用c#以编程方式从Word文档中删除一行?
我有一些代码来查找和替换word文档中的字段与数据集中的值。
Word.Document oWordDoc = new Word.Document(); foreach (Word.Field mergeField in oWordDoc.Fields) { mergeField.Select(); oWord.Selection.TypeText( stringValueFromDataSet ); }
在某些情况下, stringValueFromDataSet
为空,除了不插入任何内容外,我想实际删除当前行。
知道我怎么能这样做吗?
显然,你的答案适用于你的情况。 但是在一般情况下(无法使用退格键删除整行),可以使用以下内容:
private static object missing = System.Reflection.Missing.Value; /// /// Deletes the line in which there is a selection. /// /// Instance of Application object. 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); }
好吧,这最终是非常容易的。
oWord.Selection.TypeBackspace();//remove the field
oWord.Selection.TypeBackspace();//remove the line