ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 13968|回复: 19

[分享] 清空剪切板代码

[复制链接]

TA的精华主题

TA的得分主题

发表于 2015-7-18 00:37 | 显示全部楼层 |阅读模式
本帖已被收录到知识树中,索引项:Windows API应用
这段代码来之我们EH论谈wangminbai这位老师的博客,
以前找了好多代码都不能把剪切板清空掉,正如wangminbai老师在博客中所说,把这个代码留下以后备用吧,也许有人也在找这样的代码。
谢谢wangminbai老师。
  1. Option Explicit
  2. '********************************************************
  3. 'Module : ClearOfficeClipboard
  4. 'DateTime : 2008-4-24
  5. 'Author : Mars , http://www.excelfans.com
  6. 'Purpose : Clear Windows and Office Clipboards
  7. '********************************************************
  8. ' 声明API函数
  9. ' 查找指定窗口的子窗口
  10. Private Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
  11. ' 从窗口返回Accessible对象
  12. Private Declare Function AccessibleObjectFromWindow _
  13.     Lib "oleacc" ( _
  14.         ByVal hwnd As Long, _
  15.         ByVal dwId As Long, _
  16.         riid As tGUID, _
  17.         ppvObject As Object) _
  18. As Long
  19. ' 取得Accessible的子对象
  20. Private Declare Function AccessibleChildren _
  21.     Lib "oleacc" ( _
  22.         ByVal paccContainer As IAccessible, _
  23.         ByVal iChildStart As Long, _
  24.         ByVal cChildren As Long, _
  25.         rgvarChildren As Variant, _
  26.         pcObtained As Long) _
  27. As Long
  28. '锁定指定窗口,禁止它更新
  29. Private Declare Function LockWindowUpdate _
  30.     Lib "user32" ( _
  31.         ByVal hwndLock As Long) _
  32. As Long
  33. ' 声明类型
  34. Private Type tGUID
  35.     lData1 As Long
  36.     nData2 As Integer
  37.     nData3 As Integer
  38.     abytData4(0 To 7) As Byte
  39. End Type
  40. ' 定义常量
  41. Private Const ROLE_PUSHBUTTON = &H2B&
  42. '**********************************
  43. '***主程序,用于清除Office剪切板***
  44. '**********************************
  45. Sub ClearOfficeClipboard()
  46. ' 以下部分定义变量
  47. 100
  48.     Dim hMain As Long
  49.     Dim hExcel2 As Long
  50.     Dim hClip As Long
  51.     Dim hWindow As Long
  52.     Dim hParent As Long
  53.     Dim octl As CommandBarControl
  54.     Dim oIA As IAccessible
  55.     Dim oNewIA As IAccessible
  56.     Dim tg As tGUID
  57.     Dim lReturn As Long
  58.     Dim lStart As Long
  59.     Dim avKids() As Variant
  60.     Dim avMoreKids() As Variant
  61.     Dim lHowMany As Long
  62.     Dim lGotHowMany As Long
  63.     Dim bClip As Boolean
  64.     Dim i As Long
  65.     Dim hVersion As Long

  66.     '以下部分用于取得剪切板窗口句柄
  67.     '取得Office程序的主窗体句柄
  68.     hMain = Application.hwnd
  69.     '假如Excel版本是2000及其以下版本
  70.     hVersion = Application.Version
  71.     If hVersion < 10 Then
  72.         MsgBox "此程序不支持Excel2000及其以下版本"
  73.         Exit Sub
  74.     End If
  75.     '假如Excel版本为2007版且剪切板不可见时使其可见
  76.     If hVersion = 12 Then
  77.         bClip = True
  78.         With Application.CommandBars("Office Clipboard")
  79.             If Not .Visible Then
  80.                 LockWindowUpdate hMain
  81.                 bClip = False
  82.                 Set octl = Application.CommandBars(1).FindControl(ID:=809, recursive:=True)
  83.                 If Not octl Is Nothing Then octl.Execute
  84.             End If
  85.         End With
  86.     End If
  87.     '用于取得剪切板窗口的句柄(剪切板窗口可见时)
  88.     Do
  89.         hExcel2 = FindWindowEx(hMain, hExcel2, "EXCEL2", vbNullString)
  90.         hParent = hExcel2: hWindow = 0
  91.         hWindow = FindWindowEx(hParent, hWindow, "MsoCommandBar", vbNullString)
  92.         If hWindow Then
  93.             hParent = hWindow: hWindow = 0
  94.             hWindow = FindWindowEx(hParent, hWindow, "MsoWorkPane", vbNullString)
  95.             If hWindow Then
  96.                 hParent = hWindow: hWindow = 0
  97.                 hClip = FindWindowEx(hParent, hWindow, "bosa_sdm_XL9", "Collect and Paste 2.0")
  98.                 If hClip > 0 Then
  99.                     Exit Do
  100.                 End If
  101.             End If
  102.         End If
  103.     Loop While hExcel2 > 0

  104.     '取得剪切板窗口的句柄(剪切板窗口不可见时,2003及XP版本调用)
  105.     If hClip = 0 Then
  106.         hParent = hMain: hWindow = 0
  107.         hWindow = FindWindowEx(hParent, hWindow, "MsoWorkPane", vbNullString)
  108.         If hWindow Then
  109.             hParent = hWindow: hWindow = 0
  110.             hClip = FindWindowEx(hParent, hWindow, "bosa_sdm_XL9", "Collect and Paste 2.0")
  111.         End If
  112.     End If
  113.     '取得剪切板窗口的句柄(剪切板窗口未初始化,2003及XP版本调用)

  114.     If hClip = 0 Then
  115.         With Application.CommandBars("Task Pane")
  116.             If Not .Visible Then
  117.                 LockWindowUpdate hMain
  118.                 Set octl = Application.CommandBars(1).FindControl(ID:=809, recursive:=True)
  119.                 If Not octl Is Nothing Then octl.Execute
  120.                 .Visible = False
  121.                 LockWindowUpdate 0
  122.             End If
  123.         End With
  124.         hParent = hMain: hWindow = 0
  125.         hWindow = FindWindowEx(hParent, hWindow, "MsoWorkPane", vbNullString)
  126.         If hWindow Then
  127.             hParent = hWindow: hWindow = 0
  128.             hClip = FindWindowEx(hParent, hWindow, "bosa_sdm_XL9", "Collect and Paste 2.0")
  129.         End If
  130.     End If
  131.     '即如以上都未找到剪切板窗口,显示错误信息
  132.     If hClip = 0 Then
  133.         'MsgBox "剪切板窗口未找到"
  134.         GoTo 100
  135.         Exit Sub
  136.     End If
  137.     '以下部分用于取得"全部清空"按钮并执行它
  138.     '以下部分代码参考了《Advanced Microsoft Visual Basic 6.0 Second Edition》
  139.     '第16章Microsoft Active Accessibility部分
  140.     '定义IAccessible对象的GUID{618736E0-3C3D-11CF-810C-00AA00389B71}
  141.     With tg
  142.         .lData1 = &H618736E0
  143.         .nData2 = &H3C3D
  144.         .nData3 = &H11CF
  145.         .abytData4(0) = &H81
  146.         .abytData4(1) = &HC
  147.         .abytData4(2) = &H0
  148.         .abytData4(3) = &HAA
  149.         .abytData4(4) = &H0
  150.         .abytData4(5) = &H38
  151.         .abytData4(6) = &H9B
  152.         .abytData4(7) = &H71
  153.     End With
  154.     '从窗体返回Accessible对象
  155.     lReturn = AccessibleObjectFromWindow(hClip, 0, tg, oIA)
  156.     lStart = 0
  157.     '取得Accessible的子对象数量
  158.     lHowMany = oIA.accChildCount
  159.     ReDim avKids(lHowMany - 1) As Variant
  160.     lGotHowMany = 0
  161.     '返回Accessible的子对象
  162.     lReturn = AccessibleChildren(oIA, lStart, lHowMany, avKids(0), lGotHowMany)
  163.     For i = 0 To lGotHowMany - 1
  164.         If IsObject(avKids(i)) = True Then
  165.             If avKids(i).accName = "Collect and Paste 2.0" Then
  166.                 Set oNewIA = avKids(i)
  167.                 lHowMany = oNewIA.accChildCount
  168.                 Exit For
  169.             End If
  170.         End If
  171.     Next i
  172.     ReDim avMoreKids(lHowMany - 1) As Variant
  173.     lReturn = AccessibleChildren(oNewIA, lStart, lHowMany, avMoreKids(0), lGotHowMany)
  174.     '取得"全部清空"按钮并执行它
  175.     For i = 0 To lHowMany - 1
  176.         If IsObject(avMoreKids(i)) = False Then
  177.             If oNewIA.accName(avMoreKids(i)) = "全部清空" And _
  178.                oNewIA.accRole(avMoreKids(i)) = ROLE_PUSHBUTTON Then
  179.                 oNewIA.accDoDefaultAction (avMoreKids(i))
  180.                 Exit For
  181.             End If
  182.         End If
  183.     Next i
  184.     '如果原来Excel版本为12且剪切板不可见则恢复它
  185.     If hVersion = 12 And bClip = False Then
  186.         Application.CommandBars("Office Clipboard").Visible = bClip
  187.         LockWindowUpdate 0
  188.     End If
  189.      If Not octl Is Nothing Then octl.Execute
  190. End Sub
复制代码

TA的精华主题

TA的得分主题

发表于 2015-7-18 15:37 | 显示全部楼层
二楼是清空系统剪贴板,一楼代码是清空office剪贴板

TA的精华主题

TA的得分主题

发表于 2015-7-18 06:03 | 显示全部楼层
  1. Sub text()
  2.     Shell "C:\Windows\System32\cmd.exe " & "cmd /c @echo off | clip"
  3. End Sub
复制代码


楼主,直接调用cmd命令好像也可以清空剪切板吧

评分

1

查看全部评分

TA的精华主题

TA的得分主题

 楼主| 发表于 2015-7-18 10:23 | 显示全部楼层
huang1314wei 发表于 2015-7-18 06:03
楼主,直接调用cmd命令好像也可以清空剪切板吧

在我07与10版下测试这个方法没有用

TA的精华主题

TA的得分主题

发表于 2015-7-18 10:39 | 显示全部楼层
gdw831001 发表于 2015-7-18 10:23
在我07与10版下测试这个方法没有用

我在我的电脑上10版测试是有用的

TA的精华主题

TA的得分主题

 楼主| 发表于 2015-7-18 10:45 | 显示全部楼层
huang1314wei 发表于 2015-7-18 10:39
我在我的电脑上10版测试是有用的

可能是我10与07是盗版的吧,要么就是系统问题,,这些都无所谓了,如果有人找这样的功能时,看到这些贴子,也许对他们都有用,,以前我就是找这清空剪切板,找了好多代码都没有解决,昨天无意中看到了wangminbai老师的代码,然后在我这测试有用。就留下以后备用吧。

TA的精华主题

TA的得分主题

发表于 2015-7-18 11:11 | 显示全部楼层
gdw831001 发表于 2015-7-18 10:45
可能是我10与07是盗版的吧,要么就是系统问题,,这些都无所谓了,如果有人找这样的功能时,看到这些贴子 ...

嗯,看了一下代码,该考虑的都考虑了,确实比较完美

TA的精华主题

TA的得分主题

 楼主| 发表于 2015-7-18 23:13 | 显示全部楼层
liucqa 发表于 2015-7-18 15:37
二楼是清空系统剪贴板,一楼代码是清空office剪贴板

谢谢老师指点

TA的精华主题

TA的得分主题

发表于 2015-7-18 23:28 | 显示全部楼层
又看见这个了,其实百度就能找到的,前不久我还用这个做过一个自定义函数,专门取office上的剪切板的条目

TA的精华主题

TA的得分主题

发表于 2017-5-5 00:28 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
感谢分享,终于找到可以用的代码了。WIN8+EXCEL2010,感谢gdw831001,感谢wangminbai老师。
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-3-29 17:19 , Processed in 0.057921 second(s), 12 queries , Gzip On, Redis On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表