搜了好久才搜到这点。。
#region 光标移动
//移动光标
//光标下移3行 上移3行
object unit = Microsoft.Office.Interop.Word.WdUnits.wdLine;
object count = 3;
WordApp.Selection.MoveEnd( ref unit, ref count );
WordApp.Selection.MoveUp( ref unit, ref count, ref oMissing );
//Microsoft.Office.Interop.Word.WdUnits说明
//wdCell A cell.
//wdCharacter A character.
//wdCharacterFormatting Character formatting.
//wdColumn A column.
//wdItem The selected item.
//wdLine A line. //行
//wdParagraph A paragraph.
//wdParagraphFormatting Paragraph formatting.
//wdRow A row.
//wdScreen The screen dimensions.
//wdSection A section.
//wdSentence A sentence.
//wdStory A story.
//wdTable A table.
//wdWindow A window.
//wdWord A word.
//录制的vb宏
// ,移动光标至当前行首
// Selection.HomeKey unit:=wdLine
// '移动光标至当前行尾
// Selection.EndKey unit:=wdLine
// '选择从光标至当前行首的内容
// Selection.HomeKey unit:=wdLine, Extend:=wdExtend
// '选择从光标至当前行尾的内容
// Selection.EndKey unit:=wdLine, Extend:=wdExtend
// '选择当前行
// Selection.HomeKey unit:=wdLine
// Selection.EndKey unit:=wdLine, Extend:=wdExtend
// '移动光标至文档开始
// Selection.HomeKey unit:=wdStory
// '移动光标至文档结尾
// Selection.EndKey unit:=wdStory
// '选择从光标至文档开始的内容
// Selection.HomeKey unit:=wdStory, Extend:=wdExtend
// '选择从光标至文档结尾的内容
// Selection.EndKey unit:=wdStory, Extend:=wdExtend
// '选择文档全部内容(从WholeStory可猜出Story应是当前文档的意思)
// Selection.WholeStory
// '移动光标至当前段落的开始
// Selection.MoveUp unit:=wdParagraph
// '移动光标至当前段落的结尾
// Selection.MoveDown unit:=wdParagraph
// '选择从光标至当前段落开始的内容
// Selection.MoveUp unit:=wdParagraph, Extend:=wdExtend
// '选择从光标至当前段落结尾的内容
// Selection.MoveDown unit:=wdParagraph, Extend:=wdExtend
// '选择光标所在段落的内容
// Selection.MoveUp unit:=wdParagraph
// Selection.MoveDown unit:=wdParagraph, Extend:=wdExtend
// '显示选择区的开始与结束的位置,注意:文档第1个字符的位置是0
// MsgBox ("第" & Selection.Start & "个字符至第" & Selection.End & "个字符")
// '删除当前行
// Selection.HomeKey unit:=wdLine
// Selection.EndKey unit:=wdLine, Extend:=wdExtend
// Selection.Delete
// '删除当前段落
// Selection.MoveUp unit:=wdParagraph
// Selection.MoveDown unit:=wdParagraph, Extend:=wdExtend
// Selection.Delete
//表格的光标移动
//光标到当前光标所在表格的地单元格
WordApp.Selection.Tables[1].Cell( 1, 1 ).Select();
//unit对象定义
object unith = Microsoft.Office.Interop.Word.WdUnits.wdRow;//表格行方式
object extend = Microsoft.Office.Interop.Word.WdMovementType.wdExtend;/**//**//**extend对光标移动区域进行扩展选择
object unitu = Microsoft.Office.Interop.Word.WdUnits.wdLine;//文档行方式,可以看成表格一行.不过和wdRow有区别
object unitp = Microsoft.Office.Interop.Word.WdUnits.wdParagraph;//段落方式,对于表格可以选择到表格行后的换车符,对于跨行合并的行选择,我能找到的最简单方式
//object count = 1;//光标移动量
#endregion
}
#endregion
#region 读取Word表格中某个单元格的数据。其中的参数分别为文件名(包括路径),行号,列号。
/** <summary>
/// 读取Word表格中某个单元格的数据。其中的参数分别为文件名(包括路径),行号,列号。
/// </summary>
/// <param name="fileName">word文档</param>
/// <param name="rowIndex">行</param>
/// <param name="colIndex">列</param>
/// <returns>返回数据</returns>
public static string ReadWord_tableContentByCell( string fileName, int rowIndex, int colIndex )
{
ApplicationClass cls = null;
Document doc = null;
Table table = null;
object missing = Missing.Value;
object path = fileName;
cls = new ApplicationClass();
try
{
doc = cls.Documents.Open
( ref path, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing );
table = doc.Tables[1];
string text = table.Cell( rowIndex, colIndex ).Range.Text.ToString();
text = text.Substring( 0, text.Length - 2 ); //去除尾部的mark
return text;
}
catch( Exception ex )
{
return ex.Message;
}
finally
{
if( doc != null )
doc.Close( ref missing, ref missing, ref missing );
cls.Quit( ref missing, ref missing, ref missing );
}
}
#endregion
#region 修改word表格中指定单元格的数据
/** <summary>
/// 修改word表格中指定单元格的数据
/// </summary>
/// <param name="fileName">word文档包括路径</param>
/// <param name="rowIndex">行</param>
/// <param name="colIndex">列</param>
/// <param name="content"></param>
/// <returns></returns>
public static bool UpdateWordTableByCell( string fileName, int rowIndex, int colIndex, string content )
{
ApplicationClass cls = null;
Document doc = null;
Table table = null;
object missing = Missing.Value;
object path = fileName;
cls = new ApplicationClass();
try
{
doc = cls.Documents.Open
( ref path, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing );
table = doc.Tables[1];
//doc.Range( ref 0, ref 0 ).InsertParagraphAfter();//插入回车
table.Cell( rowIndex, colIndex ).Range.InsertParagraphAfter();//.Text = content;
return true;
}
catch
{
return false;
}
finally
{
if( doc != null )
{
doc.Close( ref missing, ref missing, ref missing );
cls.Quit( ref missing, ref missing, ref missing );
}
}
}
#endregion
#region 清楚word进程
/** <summary>
/// 清楚word进程
/// </summary>
public static void KillWordProcess()
{
System.Diagnostics.Process[] myPs;
myPs = System.Diagnostics.Process.GetProcesses();
foreach( System.Diagnostics.Process p in myPs )
{
if( p.Id != 0 )
{
string myS = "WINWORD.EXE" + p.ProcessName + " ID:" + p.Id.ToString();
try
{
if( p.Modules != null )
if( p.Modules.Count > 0 )
{
System.Diagnostics.ProcessModule pm = p.Modules[0];
myS += "\n Modules[0].FileName:" + pm.FileName;
myS += "\n Modules[0].ModuleName:" + pm.ModuleName;
myS += "\n Modules[0].FileVersionInfo:\n" + pm.FileVersionInfo.ToString();
if( pm.ModuleName.ToLower() == "winword.exe" )
p.Kill();
}
}
catch
{ }
finally
{
;
}
}
}
}
#endregion