我正在使用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我不知道我的问题出在哪里。有什么想法吗?谢谢!
发布于 2012-01-31 02:00:05
我会通过将最后一行分成两部分进行调试。
subdoc.Range (将reslt放在temp变量中)检查#1是否真的在做您期望的事情。我猜测这不会是因为end超越了documentEnd。
发布于 2012-01-31 07:54:12
最后一行需要是
rng.FormattedText = subDoc.Range(ref start, ref end).FormattedText; 也就是说,您需要将.FormattedText添加到行尾。
不能将FormattedText设置为range对象,只能将其设置为formattedText对象。
它们都是System.__ComObject类型的事实只是意味着它们都被包装在RCW中。包装器中的对象具有不同的类型。
https://stackoverflow.com/questions/9068315
复制相似问题