C# 和 Word2010:DeleteAllComments 抛出“此命令不可用。”

发布于 2025-01-03 10:15:50 字数 733 浏览 2 评论 0原文

我编写了一个小型 C# 工具,使用 COM 删除 Word 文件中的所有注释。

以前我们公司使用的是WinXP + Office2003,一切运行都很完美。

现在我们切换到Win7和Office2010(64位)。

在新的操作系统和 Office 版本中,DeleteAllComments 行会导致名为“此命令不可用。”的异常,但缺少任何进一步的解释。

Microsoft.Office.Interop.Word.Document document;
object missing = System.Reflection.Missing.Value;
document = wordInstance.Documents.Open(ref filename, ref missing, ref   ReadOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);

document.Activate();
document.AcceptAllRevisions();
document.DeleteAllComments();

其余的代码工作完美......任何人都可以帮忙吗?

提前谢谢。

问候 迈克尔

I wrote a small C# Tool deleting all comments within a word file using COM.

Formerly our company used WinXP + Office2003 and all worked perfect.

Now we switched to Win7 and Office2010 (64-bit).

With the new OS and Office version the DeleteAllComments Line leads to an exception called "This command is not available." but missing any further explaination.

Microsoft.Office.Interop.Word.Document document;
object missing = System.Reflection.Missing.Value;
document = wordInstance.Documents.Open(ref filename, ref missing, ref   ReadOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);

document.Activate();
document.AcceptAllRevisions();
document.DeleteAllComments();

The rest of the code works perfect... can anybody help?

Thnx in advance.

Greetings
Michael

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千秋岁 2025-01-10 10:15:50

我迟到了,但我也遇到了同样的问题。我发现如果文档中没有注释,命令 document.DeleteAllComments() 会引发异常。

有了这几行,我就不再例外了:

if(document.Comments.Count > 0)
    document.DeleteAllComments();

I'm late, but i had the same problem. I figured out that the command document.DeleteAllComments() throws an exception if there is no comment in the Document.

with this lines there is no more exception for me:

if(document.Comments.Count > 0)
    document.DeleteAllComments();
坠似风落 2025-01-10 10:15:50

我的问题似乎来自该公司内部非常严格的安全政策。 Office2010下不允许使用宏,普通用户也无法打开它们。

(对于我公司以外的所有人员,请检查设置:文件-->选项-->安全中心-->宏)

希望有所帮助。

My problem seems to come from the very restrictive security policies within this company. No macros allowed under Office2010 and no way for a normal user to turn them on.

(For all outside my company check the setting: File-->Options-->SecurityCenter-->Macros)

Hope that helped.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文