C#控制定位Word光标移动到任意行或者最后一行,取得光标位置等操作
2014-02-20 17:38
435 查看
转:http://blog.csdn.net/jglie/article/details/7394256
十一、上下左右移动光标位
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();
}
十一、上下左右移动光标位
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();
}
相关文章推荐
- [C#] 一款代码注释清理工具
- C# WinForm开发 取消窗体关闭按钮
- [Office][C#] NPOI、OpenXML SDK、OpenOffice.org SDK 写入资料到 EXCEL 档案[转]
- 自动化测试:Selenium webdriver 学习笔记-C#版(二)
- C#将数据转换为指定类型,支持对可空类型(Nullable类)转换方法示例
- 学习Datatable增删改
- C#获取执行程序所在的当前路径的方法
- ImageEdit 展示图片(XAML, C#)
- 在C#中解决动态计算表达式的问题(如字符串"Sin(1)+Cos(2)",执行并得出结果)
- C#多线程编程介绍——使用thread、threadpool、timer
- C# 验证数字
- C#中判断字符是否为中文
- C#反射
- [CLR via C#]13. 接口
- C# 读取CSV文件
- C#动态添加带文字图片
- C# 反射 Type Assembly类
- c#中属性和字段的区别?
- 常用的window消息值
- C#摄像头编程