Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I programmatically delete a line from a Word document using c#?

Tags:

c#

.net

ms-word

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 cases stringValueFromDataSet is empty, and in addition to inserting nothing, I want to actually delete the current line.

Any idea how I can do this?

like image 945
nailitdown Avatar asked Jan 22 '23 22:01

nailitdown


1 Answers

OK this was ridiculously easy in the end.

oWord.Selection.TypeBackspace();//remove the field
oWord.Selection.TypeBackspace();//remove the line

like image 61
nailitdown Avatar answered Jan 29 '23 22:01

nailitdown