SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a webpart . I insert InputFormTextBox into webpart.

I want save InputFormTextBox.text into word file with formating.

How do insert HTML into word document?

share|improve this question
Are you looking for a docx or doc format? The OpenXML SDK and will help you inserting text into docx documents. and might be good places to start. – Robert Kaucher Mar 10 '12 at 14:58
Thanks , I am looking for a docx format.Can I use another method other than OpenXML SDK ؟ – ar.gorgin Mar 11 '12 at 4:47
up vote 0 down vote accepted

The altChunk markup tells the consuming application to import content into the document.

I use this code:

 XNamespace w =
    XNamespace r =
    File.Copy("E:\\Test.docx", "E:\\Test3.docx");
    using (WordprocessingDocument myDoc =
       WordprocessingDocument.Open("E:\\Test3.docx", true))
        string html =
           <p>        To<em> learn</em> more a<em>bout<strong> </strong> </em><strong>ASP.</strong>NET vi<span >sit jh kj</span> h</p> <strong>,j jlkjlj;lk;</strong>
        string altChunkId = "AltChunkId1";
        MainDocumentPart mainPart = myDoc.MainDocumentPart;
        AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(
            "application/xhtml+xml", altChunkId);
        using (Stream chunkStream = chunk.GetStream(FileMode.Create, FileAccess.Write))
        using (StreamWriter stringStream = new StreamWriter(chunkStream))
        XElement altChunk = new XElement(w + "altChunk",
            new XAttribute(r + "id", altChunkId)
        XDocument mainDocumentXDoc = GetXDocument(myDoc);
            .Element(w + "body")
            .Elements(w + "p")
        SaveXDocument(myDoc, mainDocumentXDoc);

    private static void SaveXDocument(WordprocessingDocument myDoc, XDocument mainDocumentXDoc)
    // Serialize the XDocument back into the part
    using (Stream str = myDoc.MainDocumentPart.GetStream(
        FileMode.Create, FileAccess.Write))
    using (XmlWriter xw = XmlWriter.Create(str))
private static XDocument GetXDocument(WordprocessingDocument myDoc)
    // Load the main document part into an XDocument
    XDocument mainDocumentXDoc;
    using (Stream str = myDoc.MainDocumentPart.GetStream())
    using (XmlReader xr = XmlReader.Create(str))
        mainDocumentXDoc = XDocument.Load(xr);
    return mainDocumentXDoc;
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.