使用 Open XML SDK 将 .docx Word 文档添加到文字处理文档

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

我刚开始使用 Open XML,但不确定如何将文件 (.docx/.doc) 插入 文字处理文档。

我尝试了类似以下的操作,但在使用 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

可以使用 AltChunk 插入整个文档(如@VarunRathore here 所述,它允许将 HTML 或 OpenXML 等内容嵌入到另一个文档中:

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

完整代码解释如下: https://docs.microsoft.com/en-us/office/open-xml/how-to-open-and-add-text-to-a-word-processing-document

创建文档后需要正确关闭它。 如果你不想使用 using 块,你必须在创建它之后使用以下样式。 重要的是不仅要保存文档的主要部分,而且还必须关闭文档本身。 使用以下模式:

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

请注意,wDocMain 和 wDoc 是具有两个不同名称空间的对象。 一个有 Save 方法,另一个有 Close 方法。 很容易混淆。