1. 程式人生 > >關於清空Office剪下板的方法

關於清空Office剪下板的方法

OptionExplicit'|---------------------------------------------------------------------------------------|
'
|Module     : ClearOfficeClipboard                                                      |
'
|DateTime   : 2008-4-24                                                                 |
'
|Author     : wangmingbai , http://www.officefans.net/cdb/forumdisplay.php?fid=1        |

'
|Purpose    : Clear Windows and Office Clipboards                                       |
'
|---------------------------------------------------------------------------------------|
'
|--------------------------------------------------|
'
|--------------宣告API函式-------------------------|
'
|--------------------------------------------------|

'
--------------查詢指定視窗的子視窗---------------PrivateDeclareFunction FindWindowEx _
    
Lib"user32.dll" _
    
Alias"FindWindowExA" ( _
        
ByVal hWnd1 AsLong, _
        
ByVal hWnd2 AsLongByVal lpsz1 AsString, _
        
ByVal lpsz2 AsString) _
AsLong'--------------從視窗返回Accessible物件---------------PrivateDeclare
Function AccessibleObjectFromWindow _
    
Lib"oleacc" ( _
        
ByVal hwnd AsLong, _
        
ByVal dwId AsLong, _
        riid 
As tGUID, _
        ppvObject 
AsObject) _
AsLong'--------------取得Accessible的子物件---------------PrivateDeclareFunction AccessibleChildren _
    
Lib"oleacc" ( _
        
ByVal paccContainer As IAccessible, _
        
ByVal iChildStart AsLong, _
        
ByVal cChildren AsLong, _
        rgvarChildren 
As Variant, _
        pcObtained 
AsLong) _
AsLong'--------------鎖定指定視窗,禁止它更新------------PrivateDeclareFunction LockWindowUpdate _
    
Lib"user32" ( _
        
ByVal hwndLock AsLong) _
AsLong'|--------------------------------------------------|
'
|-----------------宣告型別-------------------------|
'
|--------------------------------------------------|Private Type tGUID
    lData1            
AsLong
    nData2            
AsInteger
    nData3            
AsInteger
    abytData4(
0To7AsByteEnd Type
'|--------------------------------------------------|
'
|-----------------定義常量-------------------------|
'
|--------------------------------------------------|PrivateConst ROLE_PUSHBUTTON =&H2B&'|*************************************************************|
'
|**********************主程式,用於清除Office剪下板***********|
'
|*************************************************************|Sub ClearOfficeClipboard()

    
'|--------------------------------------------------|'|----------------以下部分定義變數------------------|'|--------------------------------------------------|Dim hMain        AsLongDim hExcel2      AsLongDim hClip        AsLongDim hWindow      AsLongDim hParent      AsLongDim lParameter   AsLongDim octl         As CommandBarControl
    
Dim oIA          As IAccessible
    
Dim oNewIA       As IAccessible
    
Dim tg           As tGUID
    
Dim lReturn      AsLongDim lStart       AsLongDim avKids()     As Variant
    
Dim avMoreKids() As Variant
    
Dim lHowMany     AsLongDim lGotHowMany  AsLongDim bClip        AsBooleanDim i            AsLongDim hVersion     AsLong'|--------------------------------------------------|'|-----------以下部分用於取得剪下板視窗控制代碼---------|'|--------------------------------------------------|'/--取得Office程式的主窗體控制代碼    hMain = Application.hwnd

    
'/取得Excel的版本    hVersion = Application.Version

    
'/假如Excel版本是2000及其以下版本If hVersion <10ThenMsgBox"此程式不支援Excel2000及其以下版本"Exit Sub'/假如Excel版本為2007版且剪下板不可見時使其可見If hVersion =12Then
        bClip 
=TrueWith Application.CommandBars("Office Clipboard")
            
IfNot .Visible Then
                LockWindowUpdate hMain
                bClip 
=FalseSet octl = Application.CommandBars(1).FindControl(ID:=809, recursive:=True)
                
IfNot octl IsNothingThen octl.Execute
            
EndIfEndWithEndIf'/用於取得剪下板視窗的控制代碼(剪下板視窗可見時)Do
         hExcel2 
= FindWindowEx(hMain, hExcel2, "EXCEL2", vbNullString)
         hParent 
= hExcel2: hWindow =0
         hWindow 
= FindWindowEx(hParent, hWindow, "MsoCommandBar", vbNullString)
        
If hWindow Then
             hParent 
= hWindow: hWindow =0
             hWindow 
= FindWindowEx(hParent, hWindow, "MsoWorkPane", vbNullString)
            
If hWindow Then
                 hParent 
= hWindow: hWindow =0
                 hClip 
= FindWindowEx(hParent, hWindow, "bosa_sdm_XL9""Collect and Paste 2.0")
                
If hClip >0ThenExitDoEndIfEndIfEndIfLoopWhile hExcel2 >0'/取得剪下板視窗的控制代碼(剪下板視窗不可見時,2003及XP版本呼叫)If hClip =0Then
         hParent 
= hMain: hWindow =0
         hWindow 
= FindWindowEx(hParent, hWindow, "MsoWorkPane", vbNullString)
        
If hWindow Then
             hParent 
= hWindow: hWindow =0
             hClip 
= FindWindowEx(hParent, hWindow, "bosa_sdm_XL9""Collect and Paste 2.0")
        
EndIfEndIf'/取得剪下板視窗的控制代碼(剪下板視窗未初始化,2003及XP版本呼叫)If hClip =0ThenWith Application.CommandBars("Task Pane")
            
IfNot .Visible Then
                LockWindowUpdate hMain
                
Set octl = Application.CommandBars(1).FindControl(ID:=809, recursive:=True)
                
IfNot octl IsNothingThen octl.Execute
                .Visible 
=False
                LockWindowUpdate 
0EndIfEndWith
        hParent 
= hMain: hWindow =0
        hWindow 
= FindWindowEx(hParent, hWindow, "MsoWorkPane", vbNullString)
        
If hWindow Then
             hParent 
= hWindow: hWindow =0
             hClip 
= FindWindowEx(hParent, hWindow, "bosa_sdm_XL9""Collect and Paste 2.0")
        
EndIfEndIf'/即如以上都未找到剪下板視窗,顯示錯誤資訊If hClip =0ThenMsgBox"剪下板視窗未找到"Exit SubEndIf'|--------------------------------------------------|'|------以下部分用於取得"全部清空"按鈕並執行它------|'|--------------------------------------------------|'以下部分程式碼參考了《Advanced Microsoft Visual Basic 6.0 Second Edition》第16章Microsoft Active Accessibility部分'定義IAccessible物件的GUID{618736E0-3C3D-11CF-810C-00AA00389B71}With tg
        .lData1 
=&H618736E0
        .nData2 
=&H3C3D
        .nData3 
=&H11CF
        .abytData4(
0=&H81
        .abytData4(
1=&HC
        .abytData4(
2=&H0
        .abytData4(
3=&HAA
        .abytData4(
4=&H0
        .abytData4(
5=&H38
        .abytData4(
6=&H9B
        .abytData4(
7=&H71
    
EndWith'/從窗體返回Accessible物件    lReturn = AccessibleObjectFromWindow(hClip, 0, tg, oIA)
    lStart 
=0'/取得Accessible的子物件數量    lHowMany = oIA.accChildCount
    
ReDim avKids(lHowMany -1As Variant
    lGotHowMany 
=0'/返回Accessible的子物件    lReturn = AccessibleChildren(oIA, lStart, lHowMany, avKids(0), lGotHowMany)
    
For i =0To lGotHowMany -1If IsObject(avKids(i)) =TrueThenIf avKids(i).accName ="Collect and Paste 2.0"ThenSet oNewIA = avKids(i)
                lHowMany 
= oNewIA.accChildCount
                
ExitForEndIfEndIfNext i
    
ReDim avMoreKids(lHowMany -1As Variant
    lReturn 
= AccessibleChildren(oNewIA, lStart, lHowMany, avMoreKids(0), lGotHowMany)
    
'取得"全部清空"按鈕並執行它For i =0To lHowMany -1If IsObject(avMoreKids(i)) =FalseThenIf oNewIA.accName(avMoreKids(i)) ="全部清空"And oNewIA.accRole(avMoreKids(i)) = ROLE_PUSHBUTTON Then
                oNewIA.accDoDefaultAction (avMoreKids(i))
                
ExitForEndIfEndIfNext i
    
    
'/如果原來Excel版本為12且剪下板不可見則恢復它If hVersion =12And bClip =FalseThen Application.CommandBars("Office Clipboard").Visible = bClip: LockWindowUpdate 0End Sub