C#操作Word的一点认识

开发 后端
C#操作Word的一点认识主要向你介绍了C#操作Word的内涵,以及C#操作Word的具体的方法的应用解析。

C#操作Word的内涵:C#操作Office套件的时候都需要用到COM组件,需要在项目里添加相应的COM组件。用C#在Word里查找指定关键字的方法有两种:Selection对象和Range对象。

C#操作Word之Selection对象的用法:

  1. // C#  
  2.  internal void SelectionFind()  
  3. {  
  4.   string strFind = "find me";  
  5.   Word.Find fnd = ThisApplication.Selection.Find;  
  6.    fnd.ClearFormatting();  
  7.    fnd.Text = strFind;  
  8.   object missingValue = Type.Missing;  
  9.  
  10.   if (fnd.Execute(ref missingValue, ref missingValue,  
  11.     ref missingValue, ref missingValue, ref missingValue,  
  12.     ref missingValue, ref missingValue, ref missingValue,  
  13.     ref missingValue, ref missingValue, ref missingValue,  
  14.     ref missingValue, ref missingValue, ref missingValue,  
  15.     ref missingValue))  
  16.  {  
  17.  MessageBox.Show("Text found.");  
  18.   }  
  19.   else 
  20.   {  
  21.  MessageBox.Show("The text could not be located.");  
  22.    }  

C#操作Word之Range对象的用法:

  1. // C#  
  2.     internal void RangeFind()  
  3.    {  
  4. Word.Range rng = ThisDocument.Paragraphs[2].Range;  
  5. Word.Find fnd = rng.Find;  
  6. fnd.ClearFormatting();  
  7. object missingValue = Type.Missing;  
  8. object findStr = "find me";  
  9. if (fnd.Execute(ref findStr,   
  10. ref missingValue, ref missingValue,  
  11.     ref missingValue, ref missingValue, ref missingValue,  
  12.     ref missingValue, ref missingValue, ref missingValue,  
  13.     ref missingValue, ref missingValue, ref missingValue,  
  14.     ref missingValue, ref missingValue, ref missingValue))  
  15. {  
  16.   MessageBox.Show("Text found.");  
  17. }  
  18. else 
  19. {  
  20.   MessageBox.Show("Text not found.");  
  21.  }  
  22.   rng.Select();  
  23.  } 

C#操作Word的一点体会就向你介绍到这里,希望对你了解和学习C#操作Word有所帮助。

【编辑推荐】

  1. C#操作Word书签实例浅析
  2. C#操作Word表的实例浅析
  3. C#操作Word表格的常见操作
  4. C#操作Word表格的彪悍实例
  5. C#操作Word实用实例浅析
责任编辑:仲衡 来源: ieee.org.cn
相关推荐

2009-08-18 17:34:25

C#操作符重载应用

2009-08-19 13:49:14

C#操作注册表

2009-08-18 17:20:17

C#操作符重载

2009-08-27 17:50:09

interface接口

2009-08-13 17:52:13

C#构造函数

2009-08-18 13:06:17

C#枚举类型

2009-08-17 18:15:23

C# 枚举使用

2009-09-09 10:53:25

C# MessageB

2009-08-14 11:29:28

C#创建Windows

2009-08-28 16:30:24

C#线程

2009-08-19 10:42:08

C#操作Word表格

2009-09-03 14:28:23

C#日期格式化

2009-08-03 09:17:30

ASP.NET Ses

2009-08-19 10:16:15

C#操作Word

2009-08-19 10:25:14

C#操作Word

2009-08-19 10:46:48

C#操作Word表格

2009-06-16 13:30:32

REST无状态

2009-08-19 09:42:52

C#操作Word书签

2009-09-01 11:21:02

C#读取word内容

2009-08-19 11:34:06

C#操作Word
点赞
收藏

51CTO技术栈公众号