我如何编程删除使用C#的Word文档的线路? [英] How can I programmatically delete a line from a Word document using c#?

查看:148
本文介绍了我如何编程删除使用C#的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 );  
}

在某些情况下, 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?

推荐答案

显然,你的回答工作在你的情况。然而在一般情况下(其中,可以不使用退格被除去
整行)下面,可以用:

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);
	}

这篇关于我如何编程删除使用C#的Word文档的线路?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆