如何使用 C# 以编程方式从 Word 文档中删除一行?
我有一些代码可以查找 Word 文档中的字段并将其替换为数据集中的值。
Word.Document oWordDoc = new Word.Document();
foreach (Word.Field mergeField in oWordDoc.Fields)
{
mergeField.Select();
oWord.Selection.TypeText( stringValueFromDataSet );
}
在某些情况下,stringValueFromDataSet
为空,除了不插入任何内容之外,我还想实际删除当前行。
知道我该怎么做吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,这最终变得非常简单。
oWord.Selection.TypeBackspace();//删除字段
oWord.Selection.TypeBackspace();//删除该行
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: