发生 COMException:错误代码 = -2146824070
我正在使用 Word 的 Interop 库,但收到 COM 异常:
消息:“类型不匹配” 错误代码:-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
我不确定我的问题在这里。有什么想法吗?谢谢!
I am using the Interop library for Word, and I am getting a COM Exception:
Message: "Type Mismatch"
ErrorCode: -2146824070
Source: "Microsoft Word"
The method throwing the error is:
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);
(Error is being thrown when it tries to set the FormattedText property.)
The locals when debugging are:
docStart: 0
docEnd: 1
start: 0
end: 10
I'm not sure what my issue is here. Any ideas? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将通过将最后一行分成两部分来进行调试。
检查 #1 是否确实按照您的预期进行。我的猜测是不会,因为
end
超出了documentEnd
。I would debug by splitting the last line into 2 parts.
subdoc.Range
(putting the reslt in a temp variable)rng.FormattedText
.Check that #1 is actually doing what you expect. My guess is that it won't be because
end
goes beyonddocumentEnd
.最后一行需要是
,即您需要将
.FormattedText
添加到该行的末尾。您不能将 FormattedText 设置为范围对象,只能将其设置为 formattedText 对象。
事实上,它们都是 System.__ComObject 类型,这意味着它们都包装在 RCW 中。包装器内的对象有不同的类型。
The last line needs to be
I.e. you need to add
.FormattedText
to the end of the line.You cannot set FormattedText to a range object, you can only set it to a formattedText object.
The fact that they are both of type System.__ComObject just means they are both wrapped in a RCW. The objects inside the wrappers are of different types.