如何修改Microsoft.Office.Interop.Word代码实现无修订模式Word转HTML
The issue you're facing is that revision tracking marks are bleeding into your converted HTML because the document still has active revisions. To fix this, you need to either permanently accept all revisions first or disable revision display before saving as HTML. Here's how to adjust your code:
Core Fix: Handle Revisions Before Saving
Before calling the SaveAs method, add logic to clean up or hide revisions. Accepting all revisions is the most reliable way to ensure no trace of edit history remains in your HTML output.
Modified Full Code
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application(); Type wordType = word.GetType(); Microsoft.Office.Interop.Word.Documents docs = word.Documents; Type docsType = docs.GetType(); // aPath is the document file path object fileName = aPath; Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember( "Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { fileName, true, true } ); Type docType = doc.GetType(); // --- NEW REVISION HANDLING CODE START --- // Accept all pending revisions to permanently remove them docType.InvokeMember("AcceptAllRevisions", System.Reflection.BindingFlags.InvokeMethod, null, doc, null); // Extra safety: Disable revision tracking and hide any leftover marks docType.InvokeMember("TrackRevisions", System.Reflection.BindingFlags.SetProperty, null, doc, new object[] { false }); docType.InvokeMember("ShowRevisions", System.Reflection.BindingFlags.SetProperty, null, doc, new object[] { false }); // --- NEW REVISION HANDLING CODE END --- string ConfigPath = string.Format("{0}.html", aPath); object saveFileName = ConfigPath; docType.InvokeMember( "SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML } ); docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { null, null, null }); wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
What Each Change Does
AcceptAllRevisions: This applies every pending edit in the document, wiping out all revision marks completely. This guarantees no edit history makes it into your HTML.TrackRevisions = false: Disables future revision tracking for the current session (a redundant but safe step after accepting all revisions).ShowRevisions = false: Ensures any accidental leftover revisions won't be displayed (and thus won't be included in the HTML output).
Alternative: Hide Revisions Without Accepting
If you don't want to permanently erase revisions, you can just hide them instead. Replace the revision acceptance block with these lines:
// Hide revisions without accepting them (revisions remain in original Word doc) docType.InvokeMember("ShowRevisions", System.Reflection.BindingFlags.SetProperty, null, doc, new object[] { false }); docType.InvokeMember("DisplayRevisions", System.Reflection.BindingFlags.SetProperty, null, doc, new object[] { false });
Keep in mind this only masks the marks—revisions will still exist if someone reopens the original Word file. For clean, self-contained HTML, accepting all revisions is the better approach.
内容的提问来源于stack exchange,提问作者J.Kuai

