2017-11-26 9 views
0

Ich bin neu in VSTO und OpenXML und ich möchte ein Word-Add-in entwickeln. Dieses Add-In sollte OpenXML verwenden. Ist es also möglich, geöffnete Dokumente zu bearbeiten? Zum Beispiel habe ich Word-Dokument geöffnet, und ich möchte etwas Text mit OpenXML auf Knopfdruck ersetzen.Erstellen von Word Add-in mit OpenXML

Also ich habe diesen Code.

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); 

Und fand ich diesen Text in Word hinzufügen OpenXML mit How to: Open and add text to a word processing document (Open XML SDK)

Aber ich kann nicht herausfinden, wie sie zusammenarbeiten, um zu machen.

Kann mir jemand dabei helfen, Dank

Antwort

0

Dies ist, wie ich es gelöst:

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(); 
     } 

    } 
0

Sie so etwas tun kann;

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); 
     } 
    } 
} 

Bitte lesen Sie diesen Beitrag für weitere Details.

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

+0

Ja ich es gesehen habe, aber es does 't Arbeit, wenn ich es in meinem Code tho –

+1

@Rainman implementieren 'es does't Arbeit »Sie müssen uns mehr Details geben. Was ** speziell ** funktioniert nicht? Kompiliert es nicht (und wenn ja, was ist der Fehler)? Läuft es nicht? Es läuft, aber wirft eine Ausnahme? Bestellt es übermäßige Pizza? Etwas anderes? – mjwills

Verwandte Themen