I have some code to find and replace fields in a word document with values from a dataset.
Word.Document oWordDoc = new Word.Document();
foreach (Word.Field mergeField in oWordDoc.Fields)
{
mergeField.Select();
oWord.Selection.TypeText( stringValueFromDataSet );
}
In some ca开发者_如何学JAVAses stringValueFromDataSet
is empty, and in addition to inserting nothing, I want to actually delete the current line.
Any idea how I can do this?
OK this was ridiculously easy in the end.
oWord.Selection.TypeBackspace();//remove the field
oWord.Selection.TypeBackspace();//remove the line
Obviously, your answer works in your case. However in the general case (where the entire line cannot be removed using a backspace) the following can be used:
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);
}
精彩评论