简体   繁体   English

获取Excel异常“无法设置范围类的NumberFormat属性”

[英]Get an Excel exception “Unable to set the NumberFormat property of the Range Class”

I get this exception when I double click on one of the cells in excel through VSTO 当我通过VSTO双击excel中的一个单元格时出现此异常

My code is as follows: 我的代码如下:

Worksheet worksheet = (Worksheet)Globals.ThisAddIn.Application.ActiveSheet;
Range range = worksheet.Range[entry.Key, System.Type.Missing];
range.NumberFormat = "DD-MMM-YYYY";

It works perfectly when i use a tab key or any arrow keys 当我使用Tab键或任何箭头键时,它可以完美地工作

Any inputs will be appreciated. 任何输入将不胜感激。

It will likely be because at that point when you double click the cell, the cell is in edit state, you cannot make changes to a cell/range when in edit state. 可能是因为此时您双击该单元格,该单元格处于编辑状态,当您处于编辑状态时,您无法更改该单元格/范围。

Can you not achieve what you want by using the Sheet Selection Event, which is fired whenever a selection on a sheet changes? 您无法通过使用工作表选择事件来实现所需的功能,该事件会在工作表上的选择发生更改时触发?

Note that already clicking on a cell that has focus does not fire this event. 请注意,已经单击具有焦点的单元格不会触发此事件。

https://msdn.microsoft.com/en-us/library/office/ff194470.aspx https://msdn.microsoft.com/zh-CN/library/office/ff194470.aspx

I get this exception when I double click on one of the cells in excel through VSTO 当我通过VSTO双击excel中的一个单元格时出现此异常

You get an exception in the code because Excel is in editing mode. 由于Excel处于编辑模式,因此代码中出现异常。 Use the following code to determine the state programmatically: 使用以下代码以编程方式确定状态:

Private Function IsEditing () As Boolean
    Dim cBars As Office.CommandBars = ExcelApp.CommandBars
    Dim result As Boolean = Not cBars.GetEnabledMso("FileNewDefault")
    Marshal.ReleaseComObject(cBars)
    Return result
End Function

See How to check programmatically if the user is editing an Excel cell for more information. 有关更多信息,请参见如何以编程方式检查用户是否正在编辑Excel单元格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM