c# - 使用 VSTO 读取和修改 Word 文档的文本

标签 c# .net ms-word vsto

我正在编写 MS Word 的插件。仍处于学习 VSTO 的早期阶段。所以基本上到目前为止,我在 Word 文档中拥有的是一些用户编写的文本(完整的 Word 文档),格式如下:

Hello, my name is <%NAME%>. I am <%AGE%> years old and i live in <%COUNTRY%>.

那些 <%%> 是用户从自定义任务 Pane 中拖放的变量。

我要做的是用从某个数据文件中读取的一些实际数据值替换这些变量。但现在我正在寻找的是一种读取字符串中当前事件文档内容的方法,然后暂时用虚拟数据值替换这些变量,然后用这个替换了实际值的新文档替换具有变量值的旧文档代替 NAME、AGE 等。最好在 onSaveAs 事件中使用,但由于该事件不可用,所以我必须在 BeforeSave 事件中进行。

简而言之,我正在寻找一种方法:

  • 阅读当前文档的内容。
  • 修改那些内容。
  • 写回。

我已经在网络和 MSDN 上搜索了几个小时,但找不到任何有用的东西,或者可能无法实现它,因为我是这方面的新手。

我阅读的一些文章是:

add-in in VSTO - How to get text from Word document using Ribbon with button

How To Read & Write Text From Word Document Using VSTO In C#

Globals.ThisAddIn.Application.Selection.Text;

这只会给出选定的文本,但我需要选定和未选定的文档的所有文本。

我的 ThisAddin.cs 中的当前代码是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Word;


namespace dragdrop
{
    public partial class ThisAddIn
    {

        private void ThisAddIn_Startup(object sender, System.EventArgs e){Globals.ThisAddIn.CustomTaskPanes.Add(new OrdersListUserControl(), "Drag and Drop List Items on Word Doc").Visible = true;}       
        private void ThisAddIn_Shutdown(object sender, System.EventArgs e){}
        public void OnDropOccurred(object data) //adding dropped text on current cursor position.
        {
            Word.Selection currentSelection = Application.Selection;
            currentSelection.TypeText(data.ToString());
        }
        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);

        }


        #endregion
    }
}

我们非常欢迎任何形式的帮助/指导。谢谢。

编辑:我设法使用以下方法获取内容:

Globals.ThisAddIn.Application.ActiveDocument.Range(0, 5).Text

但问题在于,如果结束范围超过文档的总长度,它将崩溃!那么如何获取文档的长度以便将其作为第二个参数提供,或者有更好的方法吗?另外我想知道只是阅读文本并将其写回会使文本失去它的格式和样式所以有没有办法保留它以实现我想要实现的目标?非常感谢您消除所有这些困惑。

最佳答案

使用文档的内容属性获取文档范围的第一个和最后一个索引

Microsoft.Office.Interop.Word._Document oDoc = 
     Globals.ThisAddIn.Application.ActiveDocument;
Object start = oDoc.Content.Start;
Object end = oDoc.Content.End;

// select entire document 
oDoc.Range(ref start,ref end).Select();

关于c# - 使用 VSTO 读取和修改 Word 文档的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26378066/

相关文章:

c# - 如何使用存储库模式将 EF 对象转换为 WCF 模型

c# - 如何使用泛型创建流畅的界面

.net - BackgroundWorker 和 Thread 的区别?

c# - 如何确保线程安全的 ASP.net 页面访问静态对象列表

performance - 对于某些用户配置文件,在Powershell中使用Word.Application的速度非常慢

java - 在 Java 中将 PDF 转换为 Word

c# - 如何从数据行中获取主键列?

c# - Entity Framework 核心缺乏适度的 LINQ 查询支持的最佳解决方案是什么?

.net - Tibco EMS 客户端如何获得许可?

VBA:将excel数据读入word