香港新浪網 MySinaBlog
« 上一篇 | 下一篇 »
Emily | 3rd Feb 2008, 12:17 PM | API / Add-in | (2009 Reads)

判斷 Excel 單格是否處於編輯狀態

以下代碼就是當處於編輯狀態時在Excel的狀態欄上顯示 正處於編輯狀態

The code will display "Excel in edit mode ......" in Status bar if Excel is in edit mode

'---------------------------------------------------------------------------------------
' Module    : Module1
' DateTime  :
' Author    : wangmingbai
' Source    : http://hi.baidu.com/officecm/blog/item/33bd0877fe63d91fb051b9c3.html
' Purpose   : Determine Excel edit mode
'---------------------------------------------------------------------------------------


'//取得類名
'//Get Class
Private Declare Function GetClassName _
    Lib "user32" _
    Alias "GetClassNameA" ( _
        ByVal hwnd As Long, _
        ByVal lpClassName As String, _
        ByVal nMaxCount As Long) As Long

'//設置鉤子函數
'//Define Hook

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

'//結束鉤子
'//End Hook

Public Declare Function UnhookWindowsHookEx _
    Lib "user32" (ByVal hHook As Long) As Long

'//下一個鉤子
''//Next Hook
Public Declare Function CallNextHookEx _
    Lib "user32" ( _
        ByVal hHook As Long, _
        ByVal nCode As Long, _
        ByVal wParam As Long, _
        lparam As Any) As Long

'//取得當前線程的ID
'' Get Current 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
 

'-------設置鉤子-----------
'Set hook

Public Sub EnableHook()
    If IHook = 0 Then
        IThreadId = GetCurrentThreadId
        IHook = SetWindowsHookEx(WH_CBT, AddressOf HookProc, Application.Hinstance, IThreadId)
    End If
End Sub


'-------取消鉤子-----------
' Cancel Hook
Public Sub FreeHook()
    If IHook <> 0 Then
        Call UnhookWindowsHookEx(IHook)
        IHook = 0
    End If
End Sub

'---------回調----------------
'Show status

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 = "正處於編輯狀態……"
            Application.StatusBar = "Excel in Edit mode ……"
        Else
            Application.StatusBar = False
        End If
    End If
    HookProc = CallNextHookEx(IHook, nCode, wParam, lparam)
End Function


[2] Re: lisa
lisa : 您好, 我的VBA的程式中有一段迴圈是在二個檔案中做複製-貼上,程式是可以運作,但運件時視窗會在二個檔案中跳換,請問是否有指令可以讓視窗不跳換呢?

不明白, 請在 ExcelHelp 發問 (With code) , thanks
http://www.excelhelp.net/cgi-bin/forum/leoboard.cgi

 


[引用] | 作者 Emily | 18th Feb 2008 14:29 PM | [舉報垃圾留言]

[1] VBA的程式

您好, 我的VBA的程式中有一段迴圈是在二個檔案中做複製-貼上,程式是可以運作,但運件時視窗會在二個檔案中跳換,請問是否有指令可以讓視窗不跳換呢?


[引用] | 作者 lisa | 17th Feb 2008 22:04 PM | [舉報垃圾留言]