Click here to Skip to main content
16,018,805 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this code is working but not changes in docx file using openxml wordprocessing document

how to add html content in existing docx file using opexml

What I have tried:

Using myDoc As WordprocessingDocument = WordprocessingDocument.Open("C:\Users\admin\Documents\Visual Studio 2015\Projects\WebApplication6\WebApplication6\temp\FTANJS2.docx", True)

               Dim html As String = "< html ><head/><body>
               <h1>Html Heading</h1>
               <p>This Is an html document In a String literal.</p>
                </body>
                 </html>"



               Dim altChunkId As String = "AltChunkId1"
               Dim mainPart As MainDocumentPart = myDoc.MainDocumentPart
               Dim chunk As AlternativeFormatImportPart = mainPart.AddAlternativeFormatImportPart("application/xhtml+xml", altChunkId)

               Dim chunkStream As Stream = chunk.GetStream(FileMode.Create, FileAccess.Write)
               Dim stringStream As StreamWriter = New StreamWriter(chunkStream)
               stringStream.Write(html)

               Dim altChunk As AltChunk = New AltChunk
               altChunk.Id = altChunkId

               mainPart.Document.Body.InsertAfter(altChunk, mainPart.Document.Body.Elements(Of Paragraph).Last)
               mainPart.Document.Save()

           End Using
Posted
Comments
Matias Lopez 18-Sep-18 12:07pm    
Did you test that code not exit with errors? Can you add the try/catch code block in VB?
Wasim Ahmed Nov2021 23-Nov-21 13:09pm    
Hello, I have a similar situation ... did you happen to find any solution to this using OpenXML or any other opensource code?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900