C#控制定位Word光标移动到任意行或者最后一行,取得光标位置等操作

简介:

十一、上下左右移动光标位

private void moveLeft()

{

object moveUnit = Microsoft.Office.Interop.Word.WdUnits.wdWord;

object moveCount = 1;

object moveExtend = Microsoft.Office.Interop.Word.WdMovementType.wdExtend;

thisDocument.Application.Selection.MoveLeft(ref moveUnit, ref moveCount, ref MissingValue);

}

private void moveRight()

{

if(selection==null||selection!=document.Application.Selection)

selection=document.Application.Selection;

object dummy=System.Reflection.Missing.Value;

object count=1;

object Unit=Word.WdUnits.wdCharacter;

selection.MoveRight(ref Unit,ref count,ref dummy);

}

十二、取得当前光标位

public void GetCursor()

{

if(selection==null||selection!=document.Application.Selection)

selection=document.Application.Selection;

object a=selection.get_Information(Word.WdInformation.wdFirstCharacterLineNumber);

object b=selection.get_Information(Word.WdInformation.wdFirstCharacterColumnNumber);

object c=selection.get_Information(Word.WdInformation.wdActiveEndAdjustedPageNumber);

MessageBox.Show(a.ToString()+”行,”+b.ToString()+”列,”+c.ToString()+”页”);

}

十三、定位到指定行或相对行

/// <summary>

/// 定位到指定行

/// </summary>

/// <param name=”lineNum”>行号</param>

private void gotoAbsolutLine(int lineNum)

{

if(selection==null||selection!=document.Application.Selection)

selection=document.Application.Selection;

object dummy=System.Reflection.Missing.Value;

object what=Word.WdGoToItem.wdGoToLine;

object which=Word.WdGoToDirection.wdGoToFirst;

object count=lineNum;

selection.GoTo(ref what,ref which,ref count,ref dummy);

}

/// <summary>

/// 定位到相对行,例如+4

/// </summary>

/// <param name=”lineNum”>行数</param>

private void gotoOppositeLine(int lineNum)

{

if(selection==null||selection!=document.Application.Selection)

selection=document.Application.Selection;

object dummy=System.Reflection.Missing.Value;

object what=Word.WdGoToItem.wdGoToLine;

object which;

if(lineNum<0)

which=Word.WdGoToDirection.wdGoToPrevious;

else

which=Word.WdGoToDirection.wdGoToNext;

object count=Math.Abs(lineNum);

selection.GoTo(ref what,ref which,ref count,ref dummy);

}

十四、定位到文档最后一行

private void gotoLastLine(Document thisDocument)

{

object dummy = System.Reflection.Missing.Value;

object what = WdGoToItem.wdGoToLine;

object which = WdGoToDirection.wdGoToLast;

object count = 99999999;

thisDocument.Application.Selection.GoTo(ref what, ref which, ref count, ref dummy);

}

十五、定位到第一个字符

private void gotoFirstCharacter()

{

if(selection==null||selection!=document.Application.Selection)

selection=document.Application.Selection;

int oldLine=0;

gotoAbsolutLine(1);

object a=selection.get_Information(Word.WdInformation.wdFirstCharacterLineNumber);//得到当前行号

while(oldLine!=int.Parse(a.ToString()))//一直按右键,直到光标不再往下了为止

{

oldLine++;

moveRight();

a=selection.get_Information(Word.WdInformation.wdFirstCharacterLineNumber);

}

gotoAbsolutLine(int.Parse(a.ToString()));

}

十六、定位到最后一个字符

public void gotoLastCharacter()

{

if(selection==null||selection!=document.Application.Selection)

selection=document.Application.Selection;

gotoLastLine();

object dummy=System.Reflection.Missing.Value;

object count=99999999;

object Unit=Word.WdUnits.wdCharacter;

selection.MoveRight(ref Unit,ref count,ref dummy);

}

二十一、 取得行、列、页信息

public string WordGetRCP()

{

selection=document.Application.Selection;//wd.Selection;

object a=selection.get_Information(Word.WdInformation.wdFirstCharacterLineNumber);

object b=selection.get_Information(Word.WdInformation.wdFirstCharacterColumnNumber);

object c=selection.get_Information(Word.WdInformation.wdActiveEndAdjustedPageNumber);

return a.ToString()+”,”+b.ToString()+”,”+c.ToString();

}




本文转自黄聪博客园博客,原文链接:http://www.cnblogs.com/huangcong/archive/2012/02/17/2355264.html,如需转载请自行联系原作者

相关文章
|
1月前
|
C# 开发工具 数据安全/隐私保护
C# 实现 Word 加盖骑缝章效果
C# 实现 Word 加盖骑缝章效果
|
1月前
|
SQL 安全 API
C# 读取Word表格到DataSet
C# 读取Word表格到DataSet
|
11月前
|
前端开发 C#
C# 基于NPOI+Office COM组件 实现20行代码在线预览文档(word,excel,pdf,txt,png)
C# 基于NPOI+Office COM组件 实现20行代码在线预览文档(word,excel,pdf,txt,png)
|
1月前
|
存储 SQL 数据库
C# 将 Word 转文本存储到数据库并进行管理
C# 将 Word 转文本存储到数据库并进行管理
103 2
|
1月前
|
存储 SQL 数据库
C# 将 Word 转文本存储到数据库并进行管理
C# 将 Word 转文本存储到数据库并进行管理
|
1月前
|
存储 SQL C#
C# 读取二维数组集合输出到Word预设表格
C# 读取二维数组集合输出到Word预设表格
|
1月前
|
SQL C# 数据库
C# 读取多条数据记录导出到 Word 标签模板
C# 读取多条数据记录导出到 Word 标签模板
|
1月前
|
C# 开发工具 数据安全/隐私保护
C#实现基于Word保护性模板文件的修改
C#实现基于Word保护性模板文件的修改
|
1月前
|
JavaScript API C#
C# 将 Word 转化分享为电子期刊
C# 将 Word 转化分享为电子期刊