• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

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

}


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
c#导出excel的两种常见方法发布时间:2022-07-10
下一篇:
采用C#反射机制和动态参数实现可插件业务平台发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap