无法将透明代理转换为类型“Microsoft.Office.Interop.Excel.Range”
在极少数情况下,我会收到错误:System.InvalidCastException:无法将透明代理转换为类型“Microsoft.Office.Interop.Excel.Range”。我不知道如何处理/解决这个问题。它随机发生,而不是在同一行。当我尝试编写某些内容、制作边框和其他内容时,我总是投射 ((MyExcel.Range)Excel.Cells..).Value = ..,有时它只是一个字符串、数字、数组、边框,或尝试插入新行。下面的代码只是一个示例。
using MyExcel = Microsoft.Office.Interop.Excel;
...
private void xx()
try
{
MyExcel.Application Excel = Globals.ThisAddIn.Application;
((MyExcel.Range)Excel.Cells[5, 5]).Value = aaa1;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
In very rare scenarios I get error: System.InvalidCastException: Unable to cast transparent proxy to type 'Microsoft.Office.Interop.Excel.Range'. And I'm not sure how to deal/fix this. It occurs randomly, not on same row. When I try something to write, make borders and other stuff I always cast ((MyExcel.Range)Excel.Cells..).Value = .., sometimes its just a string, numbers, arrays, borders, or trying insert new rows. The Code below it's just a sample.
using MyExcel = Microsoft.Office.Interop.Excel;
...
private void xx()
try
{
MyExcel.Application Excel = Globals.ThisAddIn.Application;
((MyExcel.Range)Excel.Cells[5, 5]).Value = aaa1;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您需要打破属性和方法调用链:
首先,按以下方式声明范围对象:
现在,如果
range
对象不是null
您可以尝试调用Value
属性。First of all, you need to break the chain of property and method calls:
First, declare the range object in the following way:
Now if the
range
object is notnull
you can try to call theValue
property.