使用VSTO读取Excel中的ActiveCell内容
我正在尝试从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
R.Text.ToString();将从单元格中获取文本
R.Text.ToString(); will get you the text from the cell
不要使用活动窗口。修改你的代码如下
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.
这有什么用吗: 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.