Rueben Ramirez
Rueben Ramirez

Reputation: 13

Adding a .docx Word document to a word processing document using Open XML SDK

I just started using Open XML and am unsure how to insert a file (.docx/.doc) into a word processing document.

I've tried something like the following but get a unknown document error when launching the document with Word 2016.

Using wordprocessingDocument As WordprocessingDocument = WordprocessingDocument.Open(memoryStream, True)
    Using addProcessDoc As WordprocessingDocument = WordprocessingDocument.Open(itemView.Items(i).SubItems(3).Text.ToString, True)
        Dim tempPart = addProcessDoc.MainDocumentPart
        wordprocessingDocument.AddPart(tempPart) 'ERROR: Specified argument was out of the range of valid values.
    End Using
End Using

Upvotes: 0

Views: 2666

Answers (2)

Meisam Rasouli
Meisam Rasouli

Reputation: 321

The complete code is explained here: https://learn.microsoft.com/en-us/office/open-xml/how-to-open-and-add-text-to-a-word-processing-document

After creating the document it is necessary to close it properly. If you don't want to use using block, you have to use the following style after you have created it. It is important to not only save the main part of the document, but you also have to close the document itself too. use the following pattern:

Dim wDocFilepath as string ="C:\myWorddoc.docx"

Dim wDocMain As DocumentFormat.OpenXml.Wordprocessing.Document 
Dim wDoc As DocumentFormat.OpenXml.Packaging.WordprocessingDocument
wDoc = DocumentFormat.OpenXml.Packaging.WordprocessingDocument.Open(wDocFilepath, True)

wDocMain = wDoc.MainDocumentPart.Document

If wDoc IsNot Nothing Then
       wDocMain.Save()
       wDoc.Close()
End If

wDoc = Nothing

Pay attention that wDocMain and wDoc are objects with two different name spaces. One has a Save method and the other one has a Close method. It is easy to mix them up.

Upvotes: 0

Dirk Vollmar
Dirk Vollmar

Reputation: 176239

Inserting an entire document can be achieved using an AltChunk (as described by @VarunRathore here, which allow to embed content such as HTML or OpenXML into another document:

Imports System.Linq
Imports System.IO
Imports DocumentFormat.OpenXml.Packaging
Imports DocumentFormat.OpenXml.Wordprocessing

Class Program

    Private Shared Sub Main(args As String())

        Dim targetFileName As String = "c:\Users\Public\Documents\Target.docx"
        Dim sourceFileName As String = "c:\Users\Public\Documents\Source.docx"
        Dim templateFile As String = "c:\Users\Public\Documents\Template.docx"

        ' create target file from template
        File.Delete(targetFileName)
        File.Copy(templateFile, targetFileName)

        ' open target document
        Using myDoc As WordprocessingDocument = 
            WordprocessingDocument.Open(targetFileName, True)

            Dim altChunkId As String = "AltChunkId1"
            Dim mainPart As MainDocumentPart = myDoc.MainDocumentPart
            Dim chunk As AlternativeFormatImportPart = 
                mainPart.AddAlternativeFormatImportPart(
                    AlternativeFormatImportPartType.WordprocessingML, 
                    altChunkId)

            ' feed the source document into the alt chunk
            Using fileStream As FileStream = File.Open(sourceFileName, FileMode.Open)
                chunk.FeedData(fileStream)
            End Using

            ' insert alt chunk after last paragraph of body
            Dim altChunk As New AltChunk()
            altChunk.Id = altChunkId
            mainPart.Document.Body.InsertAfter(
                altChunk, 
                mainPart.Document.Body.Elements(Of Paragraph)().Last())

            ' save the document
            mainPart.Document.Save()
        End Using

    End Sub

End Class

Upvotes: 2

Related Questions