当前位置: 首页 > news >正文

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

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

}

相关文章:

  • Linux-ip配置
  • c#工厂模式与抽象工厂模式
  • 江南style之---黄山
  • Linux服务器操作系统基础篇
  • 网卡及MAC和PHY的区别
  • 终端I/O之终端标识
  • Hadoop中MapReduce获取命令行参数例子(11)
  • 得到Revit子窗体
  • linux命令行抓取网页快照-(xvfb+CutyCapt)
  • 在eclipse里的 flex 没有可视化的编辑
  • iOS的动态代理模式的实现
  • 关于volatile
  • 通过vbs脚本备份数据-本地到异地
  • 动态SQL和静态SQL
  • hadoop集群监控工具ambari安装
  • [NodeJS] 关于Buffer
  • create-react-app项目添加less配置
  • django开发-定时任务的使用
  • isset在php5.6-和php7.0+的一些差异
  • Java面向对象及其三大特征
  • Laravel核心解读--Facades
  • leetcode46 Permutation 排列组合
  • log4j2输出到kafka
  • Logstash 参考指南(目录)
  • SegmentFault 社区上线小程序开发频道,助力小程序开发者生态
  • SOFAMosn配置模型
  • vue 配置sass、scss全局变量
  • vue-router 实现分析
  • win10下安装mysql5.7
  • 爱情 北京女病人
  • 基于Javascript, Springboot的管理系统报表查询页面代码设计
  • 如何实现 font-size 的响应式
  • 时间复杂度与空间复杂度分析
  • 世界编程语言排行榜2008年06月(ActionScript 挺进20强)
  • 数据仓库的几种建模方法
  • 微信小程序开发问题汇总
  • 微信小程序上拉加载:onReachBottom详解+设置触发距离
  • 小程序开发中的那些坑
  • [地铁译]使用SSD缓存应用数据——Moneta项目: 低成本优化的下一代EVCache ...
  • ​MPV,汽车产品里一个特殊品类的进化过程
  • # include “ “ 和 # include < >两者的区别
  • #HarmonyOS:基础语法
  • #HarmonyOS:软件安装window和mac预览Hello World
  • #Spring-boot高级
  • $forceUpdate()函数
  • (04)Hive的相关概念——order by 、sort by、distribute by 、cluster by
  • (06)金属布线——为半导体注入生命的连接
  • (10)STL算法之搜索(二) 二分查找
  • (13)Latex:基于ΤΕΧ的自动排版系统——写论文必备
  • (二) Windows 下 Sublime Text 3 安装离线插件 Anaconda
  • (二)c52学习之旅-简单了解单片机
  • (六)c52学习之旅-独立按键
  • (四)Tiki-taka算法(TTA)求解无人机三维路径规划研究(MATLAB)
  • (算法)Game
  • (一)C语言之入门:使用Visual Studio Community 2022运行hello world