Filed under: Excel VBA 範例, 一般程序

Excel中禁止/取消禁止使用剪貼簿中的功能

by chijanzen on 七月 13th, 2008 | View: 4,439 views

Tags Share Comments (6)
索   引 H0003
主   題 Excel中禁止/取消禁止使用剪貼簿中的功能
版   本 >= 90(Office 2000)
說   明 此問題在其他的地方很多人問,本範例算是最完整的了
參   考



複製以下程式碼到Module

Code


Sub DisableCutAndPaste()    '禁止
    EnableControl 21, False   ' cut
    EnableControl 19, False   ' copy
    EnableControl 22, False   ' paste
    EnableControl 755, False  ' pastespecial
    Application.OnKey "^c", ""
    Application.OnKey "^v", ""
    Application.OnKey "+{DEL}", ""
    Application.OnKey "+{INSERT}", ""
    Application.CellDragAndDrop = False
End Sub


Sub EnableCutAndPaste()     '取消禁止
    EnableControl 21, True   ' cut
    EnableControl 19, True   ' copy
    EnableControl 22, True   ' paste
    EnableControl 755, True  ' pastespecial
    Application.OnKey "^c"
    Application.OnKey "^v"
    Application.OnKey "+{DEL}"
    Application.OnKey "+{INSERT}"
    Application.CellDragAndDrop = True
End Sub


Sub EnableControl(Id As Integer, Enabled As Boolean)
    Dim CB As CommandBar
    Dim C As CommandBarControl
    For Each CB In Application.CommandBars
        Set C = CB.FindControl(Id:=Id, recursive:=True)
        If Not C Is Nothing Then C.Enabled = Enabled
    Next
End Sub


 

 


Help

Code

 

Popularity: 8%

About the Author
    網路化名: chijanzen、中國龍、邪兵衛 經歷: 第二屆微軟社群之星 第三屆微軟「最有價值專家」 第五屆微軟「最有價值專家」
Leave a Comment »6 Comments
  • Reply » eric 六月 23, 2009

    F8看出問題的會在進入迴圈這邊,
    If Not C Is Nothing Then C.Enabled = Enabled

  • Reply » eric 六月 22, 2009

    DEAR chijanzen:
    我用2000執行的時候會出現==>Enabled方法(_'CommandBarButton'物件)失敗的訊息,在2003執行則沒有問題,是EnableControl對應的數字有問題嗎?

  • Reply » chijanzen 六月 18, 2009

    你好:

    excel 2000 應該可以用的,不過我沒excel 2000可以測試

    建議你使用F8鍵來逐步執行程式碼,然後找出哪一句程式碼有問題

  • Reply » eric 六月 18, 2009

    請問2000的版本是不是不支援?打開都會有FUNCTION的錯誤

  • Reply » chijanzen 八月 14, 2008

    你好:
    在你要執行的檔案中的 ThisWorkbook 加入以下語法即可

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    EnableCutAndPaste
    End Sub

    --------------------------------------------------------------------------------

    Private Sub Workbook_Open()
    DisableCutAndPaste
    End Sub

  • Reply » seconal 八月 14, 2008

    啟動巨集後關閉檔案
    但其它excel 檔案執行時
    無法使用copy 功能
    是否有 針對單一檔案 工作表 禁止copy 的語法

Get a GravatarLeave a Reply

Name: « Required

Email Address: « Required

Website URL: « Optional

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

About me
chijanzen 分享個人Excel VBA 學習經驗,架站心得, 日常生活記事等...

Add to Google

分類
Translator
Chinese (Simplified) flagItalian flagKorean flagEnglish flagGerman flag
French flagJapanese flagRussian flagBulgarian flagFinnish flag
相簿