Office中国论坛/Access中国论坛
标题: 【源码】判断某个excel单元格是否处于编辑状态 [打印本页]
作者: 盗梦 时间: 2015-5-3 09:56
标题: 【源码】判断某个excel单元格是否处于编辑状态
做Excel开发时需要判断excel单元格是否正处理编辑状态,寻找到相关的资料,有以下解决办法
1.DataEntryMode 属性判断
本示例检查数据输入模式是否处于打开状态,如果是则关闭之。
If (Application.DataEntryMode = xlOn) Or _
(Application.DataEntryMode = xlStrict) Then
Application.DataEntryMode = xlOff
End If
DataEntryMode 属性
返回或者设置数据输入模式,如下表所示。处于数据输入模式时,仅可在当前选定区域的未锁定单元格中输入数据。Long 类型,可读写。
值 意义
xlOn 数据输入模式处于打开状态。
xlOff 数据输入模式处于关闭状态。
xlStrict 数据输入模式处于打开状态,并且按 ESC 键时不关闭数据输入模式。
2.使用API (网上作者:wangminbai)
Excel编辑状态下,VBA代码无法运行,楼主需要考虑用VB监控OS实现
使用钩子函数应该可以判断的,以下代码就是当处于编辑状态时在Excel的状态栏上显示 正处于编辑状态……
'**********************************************************************************************************
'**********************************************************************************************************
'//取得类名
Private Declare Function GetClassName _
Lib "user32" _
Alias "GetClassNameA" ( _
ByVal hwnd As Long, _
ByVal lpClassName As String, _
ByVal nMaxCount As Long) _
As Long
'//设置钩子函数
Public Declare Function SetWindowsHookEx _
Lib "user32" _
Alias "SetWindowsHookExA" ( _
ByVal idHook As Long, _
ByVal lpfn As Long, _
ByVal hmod As Long, _
ByVal dwThreadId As Long) _
As Long
'//结束钩子
Public Declare Function UnhookWindowsHookEx _
Lib "user32" ( _
ByVal hHook As Long) _
As Long
'//下一个钩子
Public Declare Function CallNextHookEx _
Lib "user32" ( _
ByVal hHook As Long, _
ByVal nCode As Long, _
ByVal wParam As Long, _
lparam As Any) _
As Long
'//取得当前线程的ID
Public Declare Function GetCurrentThreadId _
Lib "kernel32" () _
As Long
Public Const HCBT_SETFOCUS = 9
Public Const WH_CBT = 5
Public IHook As Long
Public IThreadId As Long
Public ClassName As String
'-------设置钩子-----------
Public Sub EnableHook()
If IHook = 0 Then
IThreadId = GetCurrentThreadId
IHook = SetWindowsHookEx(WH_CBT, AddressOf HookProc, Application.Hinstance, IThreadId)
End If
End Sub
'-------取消钩子-----------
Public Sub FreeHook()
If IHook <> 0 Then
Call UnhookWindowsHookEx(IHook)
IHook = 0
End If
End Sub
'---------回调----------------
Public Function HookProc(ByVal nCode As Long, ByVal wParam As Long, ByVal lparam As Long) As Long
If nCode < 0 Then
HookProc = CallNextHookEx(IHook, nCode, wParam, lparam)
Exit Function
End If
If nCode = HCBT_SETFOCUS Then
ClassName = String(255, Chr(0))
GetClassName wParam, ClassName, 255
ClassName = Left(ClassName, InStr(ClassName, vbNullChar) - 1)
If ClassName = "EXCEL<" Or ClassName = "EXCEL6" Then
Application.StatusBar = "正处于编辑状态……"
Else
Application.StatusBar = False
End If
End If
HookProc = CallNextHookEx(IHook, nCode, wParam, lparam)
End Function
欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) |
Powered by Discuz! X3.3 |