C#With Word Interop - Comment Range.Text Always返回null [英] C# With Word Interop - Comment Range.Text Always Returns null

查看:73
本文介绍了C#With Word Interop - Comment Range.Text Always返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Windows 7,Visual Studio 2015,.NET framework 4.0(客户端配置文件)和4.6,Word 2010和Microsoft.Office.Interop.Word 14或15我无法提取评论文本来自Word文档注释 - 注释Range.Text属性始终计算
null。我的代码如下所示:

Using Windows 7, Visual Studio 2015, .NET framework 4.0 (Client Profile) and 4.6, Word 2010 and Microsoft.Office.Interop.Word 14 or 15 I am unable to extract the comment text from Word document comments - the comment Range.Text property always evaluates null. My code looks like:

using Microsoft.Office.Interop.Word;
using System.Collections.Generic;

// ...

public static List<string[]> ExtractComments(Document doc)
{
  var comments = doc.Comments;
  var commentList = new List<string[]>();
  foreach (Comment comment in comments)
  {
    var commentRow new string[4];
    string id = $"{comment.Index}";
    string author = comment.Author;
    string scope = comment.scope.Text;
    // The first three fields are present, but
    // comment.Range.Text is null unless it's viewed
    // in the debugger
    string commentText = comment.Range.Text;
    commentRow[0] = id;
    commentRow[1] = author;
    commentRow[2] = scope;
    commentRow[3] = commentText;
    commentList.Add(commentRow);
  }
  return commentList;
}





奇怪的是,如果我在调试器中检查一个Comment对象,我可以浏览到它的Range.Text属性,看看它是否存在。我是否误解了Comment接口,或者我是否可以通过编程方式完成
的实例化,这是Comment对象的动态组件?最后一点 - 使用OpenXML SDK我可以从相同的示例文档中检索所有注释文本,但是我想使用Interop来支持受密码保护的文档。 

Strangely, if I inspect a Comment object in the debugger I can browse to its Range.Text property and see that it's present. Am I misunderstanding the Comment interface, or is there something I can do programmatically to complete instantiation of the dynamic component of a Comment object? One final point - using the OpenXML SDK I am able to retrieve all comment text from the same sample documents, however I want to use Interop to support password-protected documents. 

谢谢。

推荐答案

你好Flitcraft,

Hello Flitcraft,

我没有重现你的问题,因为我没有Windows 7和Visual Studio 2015. 

I failed to reproduce your issue since I don't have Windows 7 and Visual Studio 2015. 

我尝试在Windows 10,Visual Studio 2017和Word 2010中测试您的代码。该代码对我有用,可以返回评论文本。 

I tried to test your code in Windows 10, Visual Studio 2017 and Word 2010. The code worked for me to return comment text. 

您说您使用.NET framework 4.0(Client Profile)和4.6测试了代码,那么其他版本的.net Framework呢?你有其他环境吗?如果是,您可以尝试在这些环境中进行测试吗?

You said that you tested the code with .NET framework 4.0 (Client Profile) and 4.6, what about other version .net Framework? Do you have any other environment? If yes, could you please try to test on these environments?

我会尝试使用Windows 7,Visual Studio 2015和Word 2010构建一个用于测试问题的环境。这将花费一些时间,您的耐心将非常感激。

I would try to build an environment with Windows 7, Visual Studio 2015 and Word 2010 for testing your issue. This will spend some time and your patience would be greatly appreciated.

此外,我建议您与评论共享word文档文件,以便我们可以使用它来重现您的问题,一旦我构建环境。

Besides, I would suggest you share a word document file with comments so we could use it to reproduce your issue once I build the environment.

您可以通过One Drive共享文件,然后在此处输入链接地址。感谢您的理解。

You could share the file via One Drive and then put link address here. Thanks for understanding.

最诚挚的问候,

Terry


这篇关于C#With Word Interop - Comment Range.Text Always返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆