Read the ActiveCell content in Excel using VSTO

2020-04-16 18:59发布

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.

3条回答
老娘就宠你
2楼-- · 2020-04-16 19:45

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.

查看更多
We Are One
3楼-- · 2020-04-16 19:48

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.

查看更多
冷血范
4楼-- · 2020-04-16 19:50

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

查看更多
登录 后发表回答