"Specified part does not exist in the package." exception thrown when calling DeletePart()
Exception type: InvalidOperationException
Exception message: "Specified part does not exist in the package."
Stack trace:
at System.IO.Packaging.Package.GetPart(Uri partUri) at DocumentFormat.OpenXml.Features.PackageFeatureBase.DocumentFormat.OpenXml.Packaging.IPackage.GetPart(Uri uriTarget) at DocumentFormat.OpenXml.Packaging.Builder.DelegatingPackageFeature.GetPart(Uri uriTarget) at DocumentFormat.OpenXml.Packaging.Builder.DelegatingPackageFeature.GetPart(Uri uriTarget) at DocumentFormat.OpenXml.Packaging.PackageUriHandlingExtensions.MalformedUriHandlingPackage.GetPart(Uri uriTarget) at DocumentFormat.OpenXml.Packaging.OpenXmlPart.Load(OpenXmlPackage openXmlPackage, OpenXmlPart parent, Uri uriTarget, String id) at DocumentFormat.OpenXml.Packaging.PartRelationshipsFeature.LoadReferencedPartsAndRelationships() at DocumentFormat.OpenXml.Packaging.PartRelationshipsFeature.get_RelationshipMap() at DocumentFormat.OpenXml.Packaging.PartRelationshipsFeature.DocumentFormat.OpenXml.Features.IPartRelationshipsFeature.get_Parts() at DocumentFormat.OpenXml.Packaging.OpenXmlPart.FindAllReachableParts(IDictionary
2 reachableParts) at DocumentFormat.OpenXml.Packaging.OpenXmlPart.FindAllReachableParts(IDictionary2 reachableParts) at DocumentFormat.OpenXml.Packaging.OpenXmlPart.FindAllReachableParts(IDictionary2 reachableParts) at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.FindAllReachableParts(IDictionary2 reachableParts) at DocumentFormat.OpenXml.Packaging.OpenXmlPartContainer.DeletePartCore(String id) at DocumentFormat.OpenXml.Packaging.OpenXmlPartContainer.DeletePart(String id) at Jet.Nimbus.OpenXmlUtility.AddPartToDocument(String partId, String rootElementName, SpreadsheetDocument document, String xml) in C:\repos\NimbusEngine\Jet.Nimbus\OpenXmlUtility.cs:line 212
Hello, currently seeing a somewhat strange issue and was hoping for some insight on what could be causing it.
In our code, we check to see if a CustomXml part with a certain name exists, and if it does then we go ahead and delete that part from our SpreadsheetDocument object's WorkbookPart property. But even though it exists, that exception gets thrown. If we catch, log, and ignore the exception, I can see that the part with that specific name doesn't exist anymore in WorkbookPart.ChildrenRelationshipParts. So it looks like it deletes it from there but that exception gets thrown, but unclear why.
Here is the really weird thing that I notice when debugging in Visual Studio is that if I hit a breakpoint right before the DeletePart() call and hover over my SpreadsheetDocument object and look into WorkbookPart.ChildrenRelationshipParts property and enumerate results in VS, then continue code execution, that exception above doesn't get thrown and it deletes the part just fine. I even tried to enumerate programmatically over the Parts property to see if that'll "pre-load" anything, but no avail.
Thanks for putting attention to this bug. I also filed a bug report on this. You can find it here https://github.com/dotnet/Open-XML-SDK/issues/1729.
I encountered the same error, but it occurred at the very beginning of the retrieval process. After saving the Word document again using doc.Convert(); in the Microsoft.Office.Interop.Word library, I found that the file could then be opened normally.
using (WordprocessingDocument wdDoc = WordprocessingDocument.Open(inputFile, true)) { if (wdDoc.MainDocumentPart is null || wdDoc.MainDocumentPart.Document is null || wdDoc.MainDocumentPart.Document.Body is null){ } }
Not sure—what exactly is the difference
Before conversion
<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:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w15="http://schemas.microsoft.com/office/word/2012/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" xmlns:wpsCustomData="http://www.wps.cn/officeDocument/2013/wpsCustomData" mc:Ignorable="w14 w15 wp14">
After conversion
<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex" xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex" xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex" xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex" xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex" xmlns:cx6="http://schemas.microsoft.com/office/drawing/2016/5/12/chartex" xmlns:cx7="http://schemas.microsoft.com/office/drawing/2016/5/13/chartex" xmlns:cx8="http://schemas.microsoft.com/office/drawing/2016/5/14/chartex" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:aink="http://schemas.microsoft.com/office/drawing/2016/ink" xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:oel="http://schemas.microsoft.com/office/2019/extlst" 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:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16cex="http://schemas.microsoft.com/office/word/2018/wordml/cex" xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid" xmlns:w16="http://schemas.microsoft.com/office/word/2018/wordml" xmlns:w16du="http://schemas.microsoft.com/office/word/2023/wordml/word16du" xmlns:w16sdtdh="http://schemas.microsoft.com/office/word/2020/wordml/sdtdatahash" xmlns:w16sdtfl="http://schemas.microsoft.com/office/word/2024/wordml/sdtformatlock" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" 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 w15 w16se w16cid w16 w16cex w16sdtdh w16sdtfl w16du wp14">
at System.IO.Packaging.Package.GetPart(Uri partUri) at DocumentFormat.OpenXml.Features.PackageFeatureBase.DocumentFormat.OpenXml.Packaging.IPackage.GetPart(Uri uriTarget) at DocumentFormat.OpenXml.Packaging.Builder.DelegatingPackageFeature.GetPart(Uri uriTarget) at DocumentFormat.OpenXml.Packaging.Builder.DelegatingPackageFeature.GetPart(Uri uriTarget) at DocumentFormat.OpenXml.Packaging.PackageUriHandlingExtensions.MalformedUriHandlingPackage.GetPart(Uri uriTarget) at DocumentFormat.OpenXml.Packaging.OpenXmlPart.Load(OpenXmlPackage openXmlPackage, OpenXmlPart parent, Uri uriTarget, String id) at DocumentFormat.OpenXml.Packaging.PartRelationshipsFeature.LoadReferencedPartsAndRelationships() at DocumentFormat.OpenXml.Packaging.PartRelationshipsFeature.get_RelationshipMap() at DocumentFormat.OpenXml.Packaging.PartRelationshipsFeature.DocumentFormat.OpenXml.Features.IPartRelationshipsFeature.get_Count() at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.LoadAllParts() at DocumentFormat.OpenXml.Features.StrictNamespaceExtensions.StrictNamespaceFeature.DocumentFormat.OpenXml.Features.IStrictNamespaceFeature.get_Found() at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.get_StrictRelationshipFound() at DocumentFormat.OpenXml.Packaging.OpenXmlPart.LoadDomTree[T]() at DocumentFormat.OpenXml.Packaging.MainDocumentPart.get_Document()