You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改Microsoft.Office.Interop.Word代码实现无修订模式Word转HTML

How to Convert Word to HTML Without Revision Marks Using Microsoft.Office.Interop.Word

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

  1. 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.
  2. TrackRevisions = false: Disables future revision tracking for the current session (a redundant but safe step after accepting all revisions).
  3. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:06:00