C# 操作word查找指定文本并判断查找到的文本是否为粗体。。。
附件 WordOperate.rar:http://blog.blogchina.com/upload/2005-01-25/20050125210129476.rar
/*
* Created by SharpDevelop.
* User: 云海
* Web: www.yun-hai.net blog.yun-hai.net
* Date: 2005-1-25
* Time: 19:45
* Memo: 测试环境:office 2000 + windows 2000 server
* 查找word文档中的某段文字是否为粗体
*/
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WordOperate
{
///
/// Description of MainForm.
///
public class MainForm : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox txtContent;
private System.Windows.Forms.Button btnTest;
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
[STAThread]
public static void Main(string[] args)
{
Application.Run(new MainForm());
}
#region Windows Forms Designer generated code
///
/// This method is required for Windows Forms designer support.
/// Do not change the method contents inside the source code editor. The Forms designer might
/// not be able to load this method if it was changed manually.
///
private void InitializeComponent() {
this.btnTest = new System.Windows.Forms.Button();
this.txtContent = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// btnTest
//
this.btnTest.Location = new System.Drawing.Point(152, 272);
this.btnTest.Name = "btnTest";
this.btnTest.Size = new System.Drawing.Size(168, 40);
this.btnTest.TabIndex = 1;
this.btnTest.Text = "测试";
this.btnTest.Click += new System.EventHandler(this.BtnTestClick);
//
// txtContent
//
this.txtContent.Location = new System.Drawing.Point(8, 8);
this.txtContent.Multiline = true;
this.txtContent.Name = "txtContent";
this.txtContent.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.txtContent.Size = new System.Drawing.Size(448, 248);
this.txtContent.TabIndex = 0;
this.txtContent.Text = "";
//
// MainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(464, 357);
this.Controls.Add(this.btnTest);
this.Controls.Add(this.txtContent);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "MainForm";
this.Text = "MainForm";
this.ResumeLayout(false);
}
#endregion
void BtnTestClick(object sender, System.EventArgs e)
{
Word.Application word = new Word.Application();
//Word.Document doc = null;
//打开
Object filename = @"F:\Temp\Word\bin\Debug\test.doc";
Object confirmConversions = Type.Missing;
Object readOnly = Type.Missing;
Object addToRecentFiles = Type.Missing;
Object passwordDocument = Type.Missing;
Object passwordTemplate = Type.Missing;
Object revert = Type.Missing;
Object writePasswordDocument = Type.Missing;
Object writePasswordTemplate = Type.Missing;
Object format = Type.Missing;
Object encoding = Type.Missing;
Object visible = Type.Missing;
Object openConflictDocument = Type.Missing;
Object openAndRepair = Type.Missing;
Object documentDirection = Type.Missing;
Object noEncodingDialog = Type.Missing;
word.Documents.Open(ref filename,
ref confirmConversions, ref readOnly, ref addToRecentFiles,
ref passwordDocument, ref passwordTemplate, ref revert,
ref writePasswordDocument, ref writePasswordTemplate,
ref format, ref encoding, ref visible);
//doc = word.ActiveDocument;
//查找
Word.Find fnd = word.Selection.Find;
fnd.ClearFormatting();
Object findText = "黑体测试";
Object matchCase = Type.Missing;
Object matchWholeWord = Type.Missing;
Object matchWildcards = Type.Missing;
Object matchSoundsLike = Type.Missing;
Object matchAllWordForms = Type.Missing;
Object forward = Type.Missing;
Object wrap = Type.Missing;
//Object format = Type.Missing;
Object replaceWith = Type.Missing;
Object replace = Type.Missing;
Object matchKashida = Type.Missing;
Object matchDiacritics = Type.Missing;
Object matchAlefHamza = Type.Missing;
Object matchControl = Type.Missing;
if (fnd.Execute(ref findText, ref matchCase, ref matchWholeWord,
ref matchWildcards, ref matchSoundsLike, ref matchAllWordForms,
ref forward, ref wrap, ref format, ref replaceWith,
ref replace, ref matchKashida, ref matchDiacritics,
ref matchAlefHamza, ref matchControl))
{
//发现文本
int fontBold = (int)word.Selection.Font.Bold;
if (fontBold == -1)
{
MessageBox.Show("粗体", "信息");
}
else
{
MessageBox.Show("普通", "信息");
}
}
else
{
MessageBox.Show("没找到 :(", "信息");
}
//保存退出
Object saveChanges = Word.WdSaveOptions.wdSaveChanges;
Object originalFormat = Type.Missing;
Object routeDocument = Type.Missing;
word.Quit(ref saveChanges, ref originalFormat, ref routeDocument);
}
}
}
你可以使用这个链接引用该篇文章 http://publishblog.blogchina.com/blog/tb.b?diaryID=620822