从您的应用程序编辑 Microsoft Word 文档






4.24/5 (14投票s)
使用 C# 从 Windows 应用程序窗体创建和编辑 Microsoft Word 文档。
引言
我在开发一个小工具来撰写正式信函时遇到了这个问题。我的需求是,信函模板和内容可以随时更改,应用程序只需要帮助获取输入,例如收件人姓名、组织名称、日期等。当用户按下“完成”按钮后,信函应该准备好打印,包含预定义的内容和输入。
背景
为了解决这个问题,我使用了 Microsoft VS 2005 和 C# 2.0。
以下是我遵循的步骤:
从 COM 组件中向项目添加 Microsoft.Office.Interop.Word
引用,如下所示

这将自动添加 VBIDE、Microsoft.Office.Core
以及 Microsoft.Office.Interop.Word
作为引用。
现在创建一个 Microsoft Word 文档,其中包含字段 <Name>
、<Date>
、<Subject>
和信函正文。然后将所有 3 个变量(我称这些变量为变量,因为它们将根据用户输入自动更改)放置在它们应该在的位置。我做了以下操作

现在在 VS IDE 中设计你的表单。我这样做:

Using the Code
- 现在让我们开始编码……是的……这是我的示例代码。
using Word = Microsoft.Office.Interop.Word;//<- this is what I am talking about
- 按钮背后的代码
// private void btnlettergen_Click(object sender, EventArgs e) { // create offer letter try { // Just to kill WINWORD.EXE if it is running killprocess("winword"); // copy letter format to temp.doc File.Copy("C:\\OfferLetter.doc", "c:\\temp.doc", true); // create missing object object missing = Missing.Value; // create Word application object Word.Application wordApp = new Word.ApplicationClass(); // create Word document object Word.Document aDoc = null; // create & define filename object with temp.doc object filename = "c:\\temp.doc"; // if temp.doc available if (File.Exists((string)filename)) { object readOnly = false; object isVisible = false; // make visible Word application wordApp.Visible = false; // open Word document named temp.doc aDoc = wordApp.Documents.Open(ref filename, ref missing, ref readOnly, ref missing,ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,ref isVisible, ref missing, ref missing, ref missing, ref missing); aDoc.Activate(); // Call FindAndReplace()function for each change this.FindAndReplace(wordApp, "<Date>", dtpDate.Text); this.FindAndReplace(wordApp, "<Name>", txName.Text.Trim()); this.FindAndReplace(wordApp, "<Subject>", txtSubject.Text.Trim()); // save temp.doc after modified aDoc.Save(); } else MessageBox.Show("File does not exist.", "No File", MessageBoxButtons.OK, MessageBoxIcon.Information); killprocess("winword"); } catch (Exception) { MessageBox.Show("Error in process.", "Internal Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
- 创建
FindAndReplace()
private void FindAndReplace(Word.Application wordApp, object findText, object replaceText) { object matchCase = true; object matchWholeWord = true; object matchWildCards = false; object matchSoundsLike = false; object matchAllWordForms = false; object forward = true; object format = false; object matchKashida = false; object matchDiacritics = false; object matchAlefHamza = false; object matchControl = false; object read_only = false; object visible = true; object replace = 2; object wrap = 1; wordApp.Selection.Find.Execute(ref findText, ref matchCase, ref matchWholeWord, ref matchWildCards, ref matchSoundsLike, ref matchAllWordForms, ref forward, ref wrap, ref format, ref replaceText, ref replace, ref matchKashida, ref matchDiacritics, ref matchAlefHamza, ref matchControl); }
现在检查你的 C:\ 中的 temp.doc 文件,所有用 ‘<>
’ 定义的变量都已替换为输入值。怎么样!
所以,享受你的编码……快乐编码!
历史
- 2009 年 5 月 14 日:初始发布