Read the ActiveCell content in Excel using VSTO

限于喜欢 提交于 2020-01-11 10:44:11

问题


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.


回答1:


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




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/1767338/read-the-activecell-content-in-excel-using-vsto

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!