【问题标题】:Creating word add in with OpenXML使用 OpenXML 创建词添加
【发布时间】:2023-04-02 05:34:01
【问题描述】:

我是 VSTO 和 OpenXML 的新手,我想开发一些 Word 插件。这个加载项应该使用 OpenXML,所以可以编辑打开的文档吗? 例如,我打开了 Word 文档,我想在单击按钮时使用 OpenXML 替换一些文本。

所以我有这个代码。

var fileFullName = Globals.ThisAddIn.Application.ActiveDocument.FullName;
Globals.ThisAddIn.Application.ActiveDocument.Close(WdSaveOptions.wdSaveChanges, WdOriginalFormat.wdOriginalDocumentFormat, true);

        //edit document using OpenXml here

Globals.ThisAddIn.Application.Documents.Open(fileFullName);

我发现这是使用 OpenXML 将文本添加到 Word How to: Open and add text to a word processing document (Open XML SDK)

但我不知道如何让它们一起工作。

谁能帮帮我,谢谢

【问题讨论】:

    标签: c# .net ms-word vsto openxml


    【解决方案1】:

    我就是这样解决的:

    private void button1_Click(object sender, RibbonControlEventArgs e)
            {
                var fileFullName = Globals.ThisAddIn.Application.ActiveDocument.FullName;
    
                Globals.ThisAddIn.Application.ActiveDocument.Close(WdSaveOptions.wdSaveChanges, WdOriginalFormat.wdOriginalDocumentFormat, true);
    
    
                OpenAndAddTextToWordDocument(fileFullName, "[USER_NAME]");
    
    
                Globals.ThisAddIn.Application.Documents.Open(fileFullName);
            }
    
            public static void OpenAndAddTextToWordDocument(string filepath, string txt)
            {
                // Open a WordprocessingDocument for editing using the filepath.
                WordprocessingDocument wordprocessingDocument =
                    WordprocessingDocument.Open(filepath, true);
    
                // Assign a reference to the existing document body.
                Body body = wordprocessingDocument.MainDocumentPart.Document.Body;
    
                // Add new text.
                DocumentFormat.OpenXml.Wordprocessing.Paragraph para = body.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Paragraph());
                Run run = para.AppendChild(new Run());
                run.AppendChild(new Text(txt));
    
                // Close the handle explicitly.
                wordprocessingDocument.Close();
            }
    
        }
    

    【讨论】:

      【解决方案2】:

      你可以这样做;

      public static void SearchAndReplace(string document)
      {
          using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, true))
          {
              string docText = null;
              using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
              {
                  docText = sr.ReadToEnd();
              }
      
              Regex regexText = new Regex("Hello world!");
              docText = regexText.Replace(docText, "Hi Everyone!");
      
              using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
              {
                  sw.Write(docText);
              }
          }
      }
      

      请阅读这篇文章了解更多详情。

      https://msdn.microsoft.com/en-us/library/office/bb508261.aspx

      【讨论】:

      • 是的,我已经看到了,但是当我在我的代码中实现它时它不起作用@Rainman
      • it does't work 你需要给我们更多的细节。什么具体不起作用?它没有编译(如果是,错误是什么)?它没有运行吗?它正在运行但抛出异常?是否订购过多的比萨饼?还有什么?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多