使用VSTO读取Excel中的ActiveCell内容

发布于 2024-08-12 06:13:04 字数 784 浏览 3 评论 0原文

我正在尝试从 Excel 加载项中读取 ActiveCell,但没有走得太远。有人有什么想法吗?

Excel.Window W = this.Application.ActiveWindow as Excel.Window;
Excel.Range R = W.ActiveCell as Excel.Range;
MessageBox.Show(R.Value2.ToString());

最后一行抛出的异常是:-

无法获取字段或调用方法 在类型的实例上 'Microsoft.Office.Interop.Excel.Range' 因为它是远程的代理 对象。

我尝试了 .Value,它说:-

属性、索引器或事件“值”为 不受该语言支持;尝试 直接调用访问器方法 'Microsoft.Office.Interop.Excel.Range.get_Value(对象)' 或者 'Microsoft.Office.Interop.Excel.Range.set_Value(对象, 对象)'

在尝试 get_Value() 时,我再次收到初始异常。

无法获取字段或调用方法 在类型的实例上 'Microsoft.Office.Interop.Excel.Range' 因为它是远程的代理 对象。

有什么想法吗?

干杯,

菲尔。

I'm trying to read the ActiveCell from within an Excel Add-in but not getting very far. Anyone any ideas?

Excel.Window W = this.Application.ActiveWindow as Excel.Window;
Excel.Range R = W.ActiveCell as Excel.Range;
MessageBox.Show(R.Value2.ToString());

The Exception being thrown on the last line is: -

Cannot obtain fields or call methods
on the instance of type
'Microsoft.Office.Interop.Excel.Range'
because it is a proxy to a remote
object.

I tried .Value, and it says: -

Property, indexer, or event 'Value' is
not supported by the language; try
directly calling accessor methods
'Microsoft.Office.Interop.Excel.Range.get_Value(object)'
or
'Microsoft.Office.Interop.Excel.Range.set_Value(object,
object)'

On trying get_Value() I get the initial Exception again.

Cannot obtain fields or call methods
on the instance of type
'Microsoft.Office.Interop.Excel.Range'
because it is a proxy to a remote
object.

Any ideas?

Cheers,

Phil.

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

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

发布评论

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

评论(3

舟遥客 2024-08-19 06:13:04

R.Text.ToString();将从单元格中获取文本

R.Text.ToString(); will get you the text from the cell

心的位置 2024-08-19 06:13:04

不要使用活动窗口。修改你的代码如下
Excel.Range R = this.Application.ActiveCell as Excel.Range;
if (R != null)
MessageBox.Show(R.Value2);

注意:如果用户未在活动工作表中选择单元格,则 ActiveCell 可以为 null。

Do not use Activewindow. modified your code as follows
Excel.Range R = this.Application.ActiveCell as Excel.Range;
if (R != null)
MessageBox.Show(R.Value2);

Note: ActiveCell can be null if user have not chosen a cell in the active sheet.

冰葑 2024-08-19 06:13:04

这有什么用吗: http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/f21c7cf4-fbfd-4496-a593-781eb751d580

它建议关闭代理以进行调试,暗示您的错误消息我们看到可能掩盖了较低级别的 COM 错误。

Is this of any use: http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/f21c7cf4-fbfd-4496-a593-781eb751d580

It suggests turning off proxies for debugging purposes, hinting that the error message you are seeing may be masking a lower level COM error.

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