发生COMException:ErrorCode = -2146824070

时间:2012-01-30 17:47:30

标签: c# ms-word ms-office office-interop error-code

我正在使用Word的Interop库,我收到了COM异常:

消息:“类型不匹配” ErrorCode:-2146824070 资料来源:“Microsoft Word”

抛出错误的方法是:

object docStart = doc.Content.End - 1;
object docEnd = doc.Content.End;
object start = subDoc.Content.Start;
object end = subDoc.Content.End;

Word.Range rng = doc.Range(ref docStart, ref docEnd);
rng.FormattedText = subDoc.Range(ref start, ref end);

(尝试设置FormattedText属性时会抛出错误。)

调试时的本地人是:

docStart: 0
docEnd: 1
start: 0
end: 10

我不确定我的问题在这里。有任何想法吗?谢谢!

2 个答案:

答案 0 :(得分:1)

我会通过将最后一行分成两部分进行调试。

  1. 调用subdoc.Range(将reslt放入临时变量)
  2. 将其设置为rng.FormattedText
  3. 检查#1是否真的按照您的意愿行事。我的猜测不会是因为end超出documentEnd

答案 1 :(得分:0)

最后一行需要

rng.FormattedText = subDoc.Range(ref start, ref end).FormattedText; 

即。您需要将.FormattedText添加到该行的末尾。

您无法将FormattedText设置为范围对象,只能将其设置为formattedText对象。

它们都是System .__ ComObject类型的事实只是意味着它们都包含在RCW中。包装器内的对象有不同的类型。