2

I am dynamically generating a word document and need to replace my special tags using a html content which is generated using CKeditor control. Here the word document i am using is a template which contains pre entered texts and for generating document i am using Microsoft.Office.Interop.Word. Now i can find my special tag using interop's doc.range.Find methord. But when i want to replace this tag with a HTML content without losing its style. how can i do this?
using doc.range.Find.replacement.text is not practical because it will just replace the whole tag with html text.

Example: In my word document i have a special tag like shown below

 ##<Special Conditions Frag>## 

during document generation i need to accept some text from user which is entered via ckeditor control(It will be in HTML format with body tag and all). And this html content i need to replace with above special conditions frag tag in word document.

My HTML content looks like below

<html>
 <head>
  <title></title></head>
   <body>
    <p>
 <strong>Sample text</strong>
    </p>
   </body>
</html>
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Nithin Paul
  • 2,169
  • 3
  • 33
  • 55

1 Answers1

2

From XML, to OOXML, to Interop, indeed there are a lot of possible variants.

One is to use the Clipboard to do that.

string html = Clipboard.GetText(TextDataFormat.Html);

Where you can manipulate your html string, while sending to Clipboard any html code that you want.

//save the html content to a file
File.WriteAllText(temporaryFilePath, html);

You can save it in a file and open the file, you'll see in the browser the real transformation of the html code.

If you want to implement it into Word now, you can insert that file content with:

s.Range.InsertFile(temporaryFilePath);

If you want to go through all your document, you might use a loop and check for specific place replacement

foreach (NetOffice.WordApi.InlineShape s in docWord.InlineShapes)
{
    if (s.Type==NetOffice.WordApi.Enums.WdInlineShapeType.wdInlineShapePicture &&   s.AlternativeText.Contains("|"))
    {
        //save the html content to a file
        File.WriteAllText(temporaryFilePath, html);  
        s.Range.InsertFile(temporaryFilePath);             
    }
}

That's a way to integrate HTML into Word. Another way, ugly way, is to transform your html code into a picture and insert the picture, which I wouldn't recommand that one. If you plan to extend that for Excel, it works the same way, if you try to implement html into Powerpoint, it won't work, but you can do it in 2steps. First paste your html into Excel, select the pasted content and paste it into Powerpoint, it works fine. If you paste a table, no problem, just do it and later, you write a 2nd loop to create a new table instead of that one you took from Excel into Powerpoint.

Edited: According to the error In your main you should add :

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }

The issue can be backtracked here: C# WinForms: How to set Main function STAThreadAttribute

Community
  • 1
  • 1
mike27015
  • 686
  • 1
  • 6
  • 19
  • Thanks Mike for you quick response..I have already tried using clipboard. When i use clipboard i was getting this error- Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. – Nithin Paul Jun 12 '13 at 11:56
  • 1
    Look at the edited comment. You have to set the attribute to STAThread in your main function. Also, don't forget to implement the libraries into your project. In case it doesn't fix, try to set your `wordApplication.Application.Visible=true/false;` in between try/catch – mike27015 Jun 12 '13 at 12:01
  • Thanks mike, in web application where should i specify [stathread], any idea? – Nithin Paul Jun 12 '13 at 12:04
  • Just before your main procedure. Like in the example I gave you, look at the link in same time. – mike27015 Jun 12 '13 at 12:12
  • Ok mike, let me do some work around and will let you know if it works or not. Thanks a lot for your time and effort. – Nithin Paul Jun 12 '13 at 12:16