以下是引用taller在2008-2-2 13:20:11的发言:正确。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
rKpDCzYe.rar
(9.79 KB, 下载次数: 280)
|