Fix ChangeDocumentType not removing VBA parts when converting to non-macro type#2039
Conversation
|
@dotnet-policy-service agree |
9f5dfb7 to
b50732c
Compare
|
Thanks for the feedback @twsouthwick @mikeebowen! I've reworked the implementation:
If Excel/PowerPoint need the same treatment, their feature classes can override |
There was a problem hiding this comment.
Pull request overview
Fixes Word ChangeDocumentType() conversion from macro-enabled (.docm) to non-macro (.docx/.dotx) by adding a post-conversion cleanup hook and using it to remove VBA-related parts after the main part content type is switched (addresses #618).
Changes:
- Add a generic
OnDocumentTypeChanged(...)hook toTypedPackageFeatureCollectionand invoke it afterChangeDocumentTypeInternal. - Override the hook for
WordprocessingDocumentto deleteVbaProjectPartandCustomizationPartwhen converting to non-macro types. - Add a test that creates a
.docm, converts to.docx, and asserts VBA parts are removed.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| test/DocumentFormat.OpenXml.Tests/DocxTests01.cs | Adds coverage for .docm → .docx conversion and VBA removal assertions. |
| src/DocumentFormat.OpenXml/Packaging/WordprocessingDocument.cs | Implements Word-specific cleanup after document type changes to non-macro types. |
| src/DocumentFormat.OpenXml/Packaging/TypedPackageFeatureCollection.cs | Introduces and calls a post-document-type-change hook for typed package feature collections. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
b50732c to
b460fae
Compare
…macro type When changing a macro-enabled document (.docm) to a standard document (.docx) via ChangeDocumentType(), VBA-related parts (VbaProjectPart, CustomizationPart) were carried over to the new document type, leaving orphaned content type entries. Add a virtual OnDocumentTypeChanged hook in TypedPackageFeatureCollection and override it in WordprocessingDocumentFeatures to recursively remove VBA parts when the new type is not macro-enabled. Fixes dotnet#618
|
@twsouthwick @mikeebowen Friendly follow-up, this is a reminder about latest push which addresses all review feedback. The only open question is whether to extend the VBA cleanup to Excel/PowerPoint in this PR or a separate one. Happy to proceed either way once you've had a chance to align. |
Summary
Fixes #618
When changing a macro-enabled document (
.docm) to a standard document (.docx) viaChangeDocumentType(), VBA-related parts (VbaProjectPart,VbaDataPart, etc.) were carried over to the new document type unchanged. This left orphanedvbaProjectcontent type entries in[Content_Types].xml, producing a file that:Changes
TypedPackageFeatureCollection.cs— AfterChangeDocumentTypeInternalcompletes, check if the new content type is non-macro-enabled. If so, remove parts with VBA relationship types (vbaProject,keyMapCustomizations) from the new main part.DocxTests01.cs— Added test that creates a.docmwith aVbaProjectPart, converts it to.docx, and verifies:VbaProjectPartisnullafter conversionvbaProjectcontent type is absent from the packageTest results
Test plan
ChangeDocumentType_MacroEnabledToDocument_RemovesVbaPartspassesChangeDocumentTypetests still pass