5

私は次のコードを持っています:

using (var doc = WordprocessingDocument.Open(filename, true))
{
    ....
}

ここで、stringfilenameはdocxファイルへの有効なパスです。

ただし、Openを呼び出すと、次のInvalidOperationExceptionが発生します。Specified part does not exist in the package.

編集:問題は、開こうとしているdocxファイルが無効であることが原因であると想定しているため、xmlを投稿します。

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 wp14">
    <w:body xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
        <w:p w:rsidRPr="005501CD" w:rsidR="00217D87" w:rsidP="005501CD" w:rsidRDefault="005501CD">
            <w:r>
                <w:t xml:space="preserve">Hello </w:t>
            </w:r>
            <w:r>
                <w:rPr>
                    <w:noProof />
                </w:rPr>
                <w:t>John</w:t>
            </w:r>
            <w:r>
                <w:t xml:space="preserve"> </w:t>
            </w:r>
            <w:r>
                <w:rPr>
                    <w:noProof />
                </w:rPr>
                <w:t>Smith</w:t>
            </w:r>
            <w:r>
                <w:t>,</w:t>
            </w:r>
            <w:bookmarkStart w:name="_GoBack" w:id="0" />
            <w:bookmarkEnd w:id="0" />
        </w:p>
        <w:sectPr w:rsidRPr="005501CD" w:rsidR="00217D87">
            <w:pgSz w:w="11906" w:h="16838" />
            <w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="708" w:footer="708" w:gutter="0" />
            <w:cols w:space="708" />
            <w:docGrid w:linePitch="360" />
        </w:sectPr>
    </w:body>
</w:document>

他のdocxファイルは正常に開きますが、メールマージコードのバグが原因で無効なdocxファイルが生成されていると思われます。

4

3 に答える 3

6

結局のところ、問題はxmlファイルの欠落が原因でした。他の誰かが同様の問題に遭遇した場合は、以下を確認してください。

  1. yourfile.docxの名前をyourfile.zipに変更します
  2. zipファイルを抽出し、というファイルを調べます[Content-Types].xml
  3. にリストされているすべてのxmlファイル[Content-Types].xmlがzipファイルに存在することを確認してください。

私の場合、[Content-Types].xmlファイルに次の行があります。

<Override PartName="/word/recipientData.xml" ContentType="application/vnd.ms-word.mailMergeRecipientData+xml"/>

recipientData.xmlしかし、wordディレクトリからファイルが見つかりませんでした。

于 2012-07-04T15:42:25.157 に答える
1

上記で投稿したXMLには、</w:document>終了タグがありません。これにより、ドキュメントパーツが見つからなくなり、おそらくそのエラーメッセージが表示されます。

于 2012-07-03T16:20:53.200 に答える
1

私の場合、次の行を追加せずに.pngメディアを追加しました[Content_Types].xml

<Default Extension="png" ContentType="image/png"/>
于 2019-12-20T14:17:09.407 に答える