问题
I'm writing an Excel Addin using COM Interop from .net. I have a command that pops up a dialog, and from the dialog I do some work like collecting data from the used range of several sheets. The problem is that if a cell is in edit mode, some of the calls that I need to make will throw exceptions. I would like a way of determining before-hand that Excel is in edit mode, so that I can warn the user to finish editing the cell first.
Any ideas?
回答1:
There is an Application.Ready property that is supposed to give you this information but in practice it doesn't work reliably. See here for a hackaround.
You might also want to look at setting Application.Interactive=false while your .net code is doing its stuff.
回答2:
Try this function:
Function IsInEditMode(ByRef exapp As Excel.Application) As Boolean
If exapp.Interactive = False Then
Return False
Else
Try
exapp.Interactive = False
exapp.Interactive = True
Return False
Catch
Return True
End Try
End If
End Function
回答3:
You didn't mention which language you're using. SZL's function is written in VB. Since I'm using C# I had to convert it. Worked great. Here is the equivalent C# code.
bool IsInEditMode(ref Microsoft.Office.Interop.Excel.Application exapp)
{
if (exapp.Interactive == false)
{
return false;
}
else
{
try
{
exapp.Interactive = false;
exapp.Interactive = true;
return false;
}
catch
{
return true;
}
}
}
来源:https://stackoverflow.com/questions/221984/how-to-tell-if-excel-application-is-in-cell-edit-mode