当前所在位置: 首页 > 数码科技 > 正文

如何清空 Office 的剪切板

2023-01-23 canyinms.com 【 字体:

关于清空Office剪切板的方法问题,网络上一直也有不少的说,总结下来一般有以下几种。Mhioffice教程网

如何清空 Office 的剪切板

 Mhioffice教程网

1、认为使用 Application.CutCopyMode = False 就可以了。其实不然,这只是取消剪切或复制模式并清除移动边框。并没有真正的清除剪切板上的数据。数据还是存在的。Mhioffice教程网

2、使用API函数程序,代码如下:Mhioffice教程网

Private Declare Function apiOpenClipboard Lib "user32" Alias "OpenClipboard" (ByVal hwnd As Long) As LongMhioffice教程网

Private Declare Function apiEmptyClipboard Lib "user32" Alias "EmptyClipboard" () As LongMhioffice教程网

Private Declare Function apiCloseClipboard Lib "user32" Alias "CloseClipboard" () As LongMhioffice教程网

Sub myClr()Mhioffice教程网

apiOpenClipboard (0)'打开剪切板Mhioffice教程网

apiEmptyClipboard'清空剪切板Mhioffice教程网

apiCloseClipboard'关闭剪切板Mhioffice教程网

End SubMhioffice教程网

但是你会发现使用这段程序也不会清空Office的剪切板,这是因为Windows不会参与剪切板私有数据的管理。Mhioffice教程网

3、前一段时间我看到一段代码,是用来清空Office的剪切板的,代码如下:Mhioffice教程网

'---------------------------------------------------------------------------------------Mhioffice教程网

' Module : Module1Mhioffice教程网

' Purpose : Clear Windows and Office ClipboardsMhioffice教程网

'---------------------------------------------------------------------------------------Mhioffice教程网

Private Declare Function apiOpenClipboard Lib "user32" Alias "OpenClipboard" (ByVal hwnd As Long) As LongMhioffice教程网

Private Declare Function apiEmptyClipboard Lib "user32" Alias "EmptyClipboard" () As LongMhioffice教程网

Private Declare Function apiCloseClipboard Lib "user32" Alias "CloseClipboard" () As LongMhioffice教程网

Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)Mhioffice教程网

Private Declare Function FindWindowEx Lib "user32.dll" _Mhioffice教程网

Alias "FindWindowExA" (ByVal hWnd1 As Long, _Mhioffice教程网

ByVal hWnd2 As Long, ByVal lpsz1 As String, _Mhioffice教程网

ByVal lpsz2 As String) As LongMhioffice教程网

Private Declare Function PostMessage Lib "user32.dll" Alias _Mhioffice教程网

"PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _Mhioffice教程网

ByVal wParam As Long, ByVal lParam As Long) As LongMhioffice教程网

Private Const WM_LBUTTONDOWN As Long = &H201&Mhioffice教程网

Private Const WM_LBUTTONUP As Long = &H202&Mhioffice教程网

' Creates a long variable out of two wordsMhioffice教程网

Private Function MakeLong(ByVal nLoWord As Integer, ByVal nHiWord As Integer) As LongMhioffice教程网

MakeLong = nHiWord * 65536 + nLoWordMhioffice教程网

End FunctionMhioffice教程网

Sub ClearOfficeClipboard()Mhioffice教程网

Dim hMain&, hExcel2&, hClip&, hWindow&, hParent&Mhioffice教程网

Dim lParameter&, sTask$Mhioffice教程网

sTask = Application.CommandBars("Task Pane").NameLocalMhioffice教程网

' Handle for XLMAINMhioffice教程网

hMain = Application.hwndMhioffice教程网

' Find the OfficeClipboard WindowMhioffice教程网

' 2 methods as we're not sure if it's visibleMhioffice教程网

' ONCE it has been made visible the windowclass is createdMhioffice教程网

' and remains loaded for the duration of the instanceMhioffice教程网

DoMhioffice教程网

hExcel2 = FindWindowEx(hMain, hExcel2, "EXCEL2", vbNullString)Mhioffice教程网

hParent = hExcel2: hWindow = 0Mhioffice教程网

hWindow = FindWindowEx(hParent, hWindow, "MsoCommandBar", sTask)Mhioffice教程网

If hWindow ThenMhioffice教程网

hParent = hWindow: hWindow = 0Mhioffice教程网

hWindow = FindWindowEx(hParent, hWindow, "MsoWorkPane", vbNullString)Mhioffice教程网

If hWindow ThenMhioffice教程网

hParent = hWindow: hWindow = 0Mhioffice教程网

hClip = FindWindowEx(hParent, hWindow, "bosa_sdm_XL9", vbNullString)Mhioffice教程网

If hClip > 0 ThenMhioffice教程网

Exit DoMhioffice教程网

End IfMhioffice教程网

End IfMhioffice教程网

End IfMhioffice教程网

Loop While hExcel2 > 0Mhioffice教程网

If hClip = 0 ThenMhioffice教程网

hParent = hMain: hWindow = 0Mhioffice教程网

hWindow = FindWindowEx(hParent, hWindow, "MsoWorkPane", vbNullString)Mhioffice教程网

If hWindow ThenMhioffice教程网

hParent = hWindow: hWindow = 0Mhioffice教程网

hClip = FindWindowEx(hParent, hWindow, "bosa_sdm_XL9", vbNullString)Mhioffice教程网

End IfMhioffice教程网

End IfMhioffice教程网

If hClip = 0 ThenMhioffice教程网

ClipWindowForceMhioffice教程网

hParent = hMain: hWindow = 0Mhioffice教程网

hWindow = FindWindowEx(hParent, hWindow, "MsoWorkPane", vbNullString)Mhioffice教程网

If hWindow ThenMhioffice教程网

hParent = hWindow: hWindow = 0Mhioffice教程网

hClip = FindWindowEx(hParent, hWindow, "bosa_sdm_XL9", vbNullString)Mhioffice教程网

End IfMhioffice教程网

End IfMhioffice教程网

If hClip = 0 ThenMhioffice教程网

MsgBox "Cant find Clipboard window"Mhioffice教程网

Exit SubMhioffice教程网

End IfMhioffice教程网

lParameter = MakeLong(120, 18)Mhioffice教程网

Call PostMessage(hClip, WM_LBUTTONDOWN, 0&, lParameter)Mhioffice教程网

Call PostMessage(hClip, WM_LBUTTONUP, 0&, lParameter)Mhioffice教程网

Sleep 100Mhioffice教程网

DoEventsMhioffice教程网

End SubMhioffice教程网

Sub ClipWindowForce()Mhioffice教程网

Dim octlMhioffice教程网

With Application.CommandBars("Task Pane")Mhioffice教程网

If Not .Visible ThenMhioffice教程网

Application.ScreenUpdating = FalseMhioffice教程网

Set octl = Application.CommandBars(1).FindControl(ID:=809, recursive:=True)Mhioffice教程网

If Not octl Is Nothing Then octl.ExecuteMhioffice教程网

.Visible = FalseMhioffice教程网

Application.ScreenUpdating = TrueMhioffice教程网

End IfMhioffice教程网

End WithMhioffice教程网

End SubMhioffice教程网

' Main program to clear Windows and Office ClipboardsMhioffice教程网

Sub myClr()Mhioffice教程网

Call ClearOfficeClipboardMhioffice教程网

apiOpenClipboard (0)Mhioffice教程网

apiEmptyClipboardMhioffice教程网

apiCloseClipboardMhioffice教程网

Application.CutCopyMode = FalseMhioffice教程网

End SubMhioffice教程网

代码的基本原理是找到剪切板窗体的句柄,然后向窗体发送一个在“全部清空”这个按钮的位置点击鼠标左键的消息,以此来清空剪切板。一开始我对这段代码进行了检测,发现他很有效果,真的可以清空Office的剪切板。并把它转载到了我的博客。但是后来我发现当剪切板的位置变为横放或者Office剪切板的大小发生变化导致“全部清空”的位置发生变化更有可能导致“全部清空”按钮不可见时,此代码就不能清空Office的剪切板(前两种情况可以用判断一下Office大小及位置的方法来发送消息来解决,但Office剪切板"全部清空"按钮不可见时就不好弄了)。可见以上的方法都不完美,不能真正的清空Office剪切板。Mhioffice教程网

前一段时间在网上找到一个小工具,名叫AccExplorer32,这个工具却可以取得Office剪切板中按钮的位置、名称等各种信息并可以对按钮进行操作。对这个东西的具体操作原理很感兴趣。但是到网上进行各种搜索都没有发现具体的实现方法。最近到网上下载了一本电子书籍名叫《Advanced Microsoft Visual Basic 6.0 Second Edition》电子书。在其中的第16章中有名叫Microsoft Active Accessibility的一节,看到之后才明白了其中的奥妙。这一节就描述怎样在VB中使用Accessibility界面。真是赶到非常的高兴。赶紧试着使用了一下。发现使用微软的Active Accessibility就可以找到“全部清空”按钮并执行它,进而达到清空Office剪切板的目的。以下就是我使用Active Accessibility来清空剪切板的代码,大家共享:Mhioffice教程网

Option ExplicitMhioffice教程网

'********************************************************Mhioffice教程网

'Module : ClearOfficeClipboardMhioffice教程网

'Purpose : Clear Windows and Office ClipboardsMhioffice教程网

'********************************************************Mhioffice教程网

' 声明API函数Mhioffice教程网

' 查找指定窗口的子窗口Mhioffice教程网

Private Declare Function FindWindowEx _Mhioffice教程网

Lib "user32.dll" _Mhioffice教程网

Alias "FindWindowExA" ( _Mhioffice教程网

ByVal hWnd1 As Long, _Mhioffice教程网

ByVal hWnd2 As Long,Mhioffice教程网

ByVal lpsz1 As String, _Mhioffice教程网

ByVal lpsz2 As String) _Mhioffice教程网

As LongMhioffice教程网

' 从窗口返回Accessible对象Mhioffice教程网

Private Declare Function AccessibleObjectFromWindow _Mhioffice教程网

Lib "oleacc" ( _Mhioffice教程网

ByVal hwnd As Long, _Mhioffice教程网

ByVal dwId As Long, _Mhioffice教程网

riid As tGUID, _Mhioffice教程网

ppvObject As Object) _Mhioffice教程网

As LongMhioffice教程网

' 取得Accessible的子对象Mhioffice教程网

Private Declare Function AccessibleChildren _Mhioffice教程网

Lib "oleacc" ( _Mhioffice教程网

ByVal paccContainer As IAccessible, _Mhioffice教程网

ByVal iChildStart As Long, _Mhioffice教程网

ByVal cChildren As Long, _Mhioffice教程网

rgvarChildren As Variant, _Mhioffice教程网

pcObtained As Long) _Mhioffice教程网

As LongMhioffice教程网

'锁定指定窗口,禁止它更新Mhioffice教程网

Private Declare Function LockWindowUpdate _Mhioffice教程网

Lib "user32" ( _Mhioffice教程网

ByVal hwndLock As Long) _Mhioffice教程网

As LongMhioffice教程网

' 声明类型Mhioffice教程网

Private Type tGUIDMhioffice教程网

lData1 As LongMhioffice教程网

nData2 As IntegerMhioffice教程网

nData3 As IntegerMhioffice教程网

abytData4(0 To 7) As ByteMhioffice教程网

End TypeMhioffice教程网

' 定义常量Mhioffice教程网

Private Const ROLE_PUSHBUTTON = &H2B&Mhioffice教程网

'**********************************Mhioffice教程网

'***主程序,用于清除Office剪切板***Mhioffice教程网

'**********************************Mhioffice教程网

Sub ClearOfficeClipboard()Mhioffice教程网

' 以下部分定义变量Mhioffice教程网

Dim hMain As LongMhioffice教程网

Dim hExcel2 As LongMhioffice教程网

Dim hClip As LongMhioffice教程网

Dim hWindow As LongMhioffice教程网

Dim hParent As LongMhioffice教程网

Dim octl As CommandBarControlMhioffice教程网

Dim oIA As IAccessibleMhioffice教程网

Dim oNewIA As IAccessibleMhioffice教程网

Dim tg As tGUIDMhioffice教程网

Dim lReturn As LongMhioffice教程网

Dim lStart As LongMhioffice教程网

Dim avKids() As VariantMhioffice教程网

Dim avMoreKids() As VariantMhioffice教程网

Dim lHowMany As LongMhioffice教程网

Dim lGotHowMany As LongMhioffice教程网

Dim bClip As BooleanMhioffice教程网

Dim i As LongMhioffice教程网

Dim hVersion As LongMhioffice教程网

 Mhioffice教程网

'以下部分用于取得剪切板窗口句柄Mhioffice教程网

'取得Office程序的主窗体句柄Mhioffice教程网

hMain = Application.hwndMhioffice教程网

'假如Excel版本是2000及其以下版本Mhioffice教程网

hVersion = Application.VersionMhioffice教程网

If hVersion < 10 ThenMhioffice教程网

MsgBox "此程序不支持Excel2000及其以下版本"Mhioffice教程网

Exit SubMhioffice教程网

End IfMhioffice教程网

'假如Excel版本为2007版且剪切板不可见时使其可见Mhioffice教程网

If hVersion = 12 ThenMhioffice教程网

bClip = TrueMhioffice教程网

With Application.CommandBars("Office Clipboard")Mhioffice教程网

If Not .Visible ThenMhioffice教程网

LockWindowUpdate hMainMhioffice教程网

bClip = FalseMhioffice教程网

Set octl = Application.CommandBars(1).FindControl(ID:=809, recursive:=True)Mhioffice教程网

If Not octl Is Nothing Then octl.ExecuteMhioffice教程网

End IfMhioffice教程网

End WithMhioffice教程网

End IfMhioffice教程网

'用于取得剪切板窗口的句柄(剪切板窗口可见时)Mhioffice教程网

DoMhioffice教程网

hExcel2 = FindWindowEx(hMain, hExcel2, "EXCEL2", vbNullString)Mhioffice教程网

hParent = hExcel2: hWindow = 0Mhioffice教程网

hWindow = FindWindowEx(hParent, hWindow, "MsoCommandBar", vbNullString)Mhioffice教程网

If hWindow ThenMhioffice教程网

hParent = hWindow: hWindow = 0Mhioffice教程网

hWindow = FindWindowEx(hParent, hWindow, "MsoWorkPane", vbNullString)Mhioffice教程网

If hWindow ThenMhioffice教程网

hParent = hWindow: hWindow = 0Mhioffice教程网

hClip = FindWindowEx(hParent, hWindow, "bosa_sdm_XL9", "Collect and Paste 2.0")Mhioffice教程网

If hClip > 0 ThenMhioffice教程网

Exit DoMhioffice教程网

End IfMhioffice教程网

End IfMhioffice教程网

End IfMhioffice教程网

Loop While hExcel2 > 0Mhioffice教程网

'取得剪切板窗口的句柄(剪切板窗口不可见时,2003及XP版本调用)Mhioffice教程网

If hClip = 0 ThenMhioffice教程网

hParent = hMain: hWindow = 0Mhioffice教程网

hWindow = FindWindowEx(hParent, hWindow, "MsoWorkPane", vbNullString)Mhioffice教程网

If hWindow ThenMhioffice教程网

hParent = hWindow: hWindow = 0Mhioffice教程网

hClip = FindWindowEx(hParent, hWindow, "bosa_sdm_XL9", "Collect and Paste 2.0")Mhioffice教程网

End IfMhioffice教程网

End IfMhioffice教程网

'取得剪切板窗口的句柄(剪切板窗口未初始化,2003及XP版本调用)Mhioffice教程网

If hClip = 0 ThenMhioffice教程网

With Application.CommandBars("Task Pane")Mhioffice教程网

If Not .Visible ThenMhioffice教程网

LockWindowUpdate hMainMhioffice教程网

Set octl = Application.CommandBars(1).FindControl(ID:=809, recursive:=True)Mhioffice教程网

If Not octl Is Nothing Then octl.ExecuteMhioffice教程网

.Visible = FalseMhioffice教程网

LockWindowUpdate 0Mhioffice教程网

End IfMhioffice教程网

End WithMhioffice教程网

hParent = hMain: hWindow = 0Mhioffice教程网

hWindow = FindWindowEx(hParent, hWindow, "MsoWorkPane", vbNullString)Mhioffice教程网

If hWindow ThenMhioffice教程网

hParent = hWindow: hWindow = 0Mhioffice教程网

hClip = FindWindowEx(hParent, hWindow, "bosa_sdm_XL9", "Collect and Paste 2.0")Mhioffice教程网

End IfMhioffice教程网

End IfMhioffice教程网

'即如以上都未找到剪切板窗口,显示错误信息Mhioffice教程网

If hClip = 0 ThenMhioffice教程网

MsgBox "剪切板窗口未找到"Mhioffice教程网

Exit SubMhioffice教程网

End IfMhioffice教程网

'以下部分用于取得"全部清空"按钮并执行它Mhioffice教程网

'以下部分代码参考了《Advanced Microsoft Visual Basic 6.0 Second Edition》Mhioffice教程网

'第16章Microsoft Active Accessibility部分Mhioffice教程网

'定义IAccessible对象的GUID{618736E0-3C3D-11CF-810C-00AA00389B71}Mhioffice教程网

With tgMhioffice教程网

.lData1 = &H618736E0Mhioffice教程网

.nData2 = &H3C3DMhioffice教程网

.nData3 = &H11CFMhioffice教程网

.abytData4(0) = &H81Mhioffice教程网

.abytData4(1) = &HCMhioffice教程网

.abytData4(2) = &H0Mhioffice教程网

.abytData4(3) = &HAAMhioffice教程网

.abytData4(4) = &H0Mhioffice教程网

.abytData4(5) = &H38Mhioffice教程网

.abytData4(6) = &H9BMhioffice教程网

.abytData4(7) = &H71Mhioffice教程网

End WithMhioffice教程网

'从窗体返回Accessible对象Mhioffice教程网

lReturn = AccessibleObjectFromWindow(hClip, 0, tg, oIA)Mhioffice教程网

lStart = 0Mhioffice教程网

'取得Accessible的子对象数量Mhioffice教程网

lHowMany = oIA.accChildCountMhioffice教程网

ReDim avKids(lHowMany - 1) As VariantMhioffice教程网

lGotHowMany = 0Mhioffice教程网

'返回Accessible的子对象Mhioffice教程网

lReturn = AccessibleChildren(oIA, lStart, lHowMany, avKids(0), lGotHowMany)Mhioffice教程网

For i = 0 To lGotHowMany - 1Mhioffice教程网

If IsObject(avKids(i)) = True ThenMhioffice教程网

If avKids(i).accName = "Collect and Paste 2.0" ThenMhioffice教程网

Set oNewIA = avKids(i)Mhioffice教程网

lHowMany = oNewIA.accChildCountMhioffice教程网

Exit ForMhioffice教程网

End IfMhioffice教程网

End IfMhioffice教程网

Next iMhioffice教程网

ReDim avMoreKids(lHowMany - 1) As VariantMhioffice教程网

lReturn = AccessibleChildren(oNewIA, lStart, lHowMany, avMoreKids(0), lGotHowMany)Mhioffice教程网

'取得"全部清空"按钮并执行它Mhioffice教程网

For i = 0 To lHowMany - 1Mhioffice教程网

If IsObject(avMoreKids(i)) = False ThenMhioffice教程网

If oNewIA.accName(avMoreKids(i)) = "全部清空" And _Mhioffice教程网

oNewIA.accRole(avMoreKids(i)) = ROLE_PUSHBUTTON ThenMhioffice教程网

oNewIA.accDoDefaultAction (avMoreKids(i))Mhioffice教程网

Exit ForMhioffice教程网

End IfMhioffice教程网

End IfMhioffice教程网

Next iMhioffice教程网

'如果原来Excel版本为12且剪切板不可见则恢复它Mhioffice教程网

If hVersion = 12 And bClip = False ThenMhioffice教程网

Application.CommandBars("Office Clipboard").Visible = bClipMhioffice教程网

LockWindowUpdate 0Mhioffice教程网

End IfMhioffice教程网

End SubMhioffice教程网

这个代码的原理是首先找到Office剪切板的句柄,然后通过Microsoft Active Accessibility来取得“全部清空”按钮并执行它,从而清空了剪切板。这也就避免了前面第3种方法的局限性。Mhioffice教程网

阅读全文
相关推荐

如何自制蛋挞

如何自制蛋挞
1、倒入低筋面、黄油和水,揉成面团状,藏一小时后取出,抹上黄油卷起,包上保鲜膜冷藏半小时。2、取出,将面团切成一厘米的面团,放入模具内,用力按压,中间稍薄,外缘要比模具高,将蛋挞液倒入蛋挞皮中,放入烤箱里烤30分钟即可。

肉火烧面怎么和面

肉火烧面怎么和面
1、首先用温水把酵母融开,加入面粉,用手把面粉揉成非常软的面团,可以放一点熬好的猪油在面团里面。2、然后放在温暖的地方,盖上保鲜膜发酵至2倍大,发好的面团用手插入不回缩,就说明面团发酵好了。

奶茶几分糖好喝

奶茶几分糖好喝
1、不同的人对奶茶的口感要求是不一样的,一般来说,奶茶有三分糖、五分糖、七分糖三种不同的口味。2、女生一般都喜欢喝甜一点的,而男生则喜欢喝不那么甜的,七分糖的奶茶最好喝。

过桥米线是哪里的?

过桥米线是哪里的?
过桥米线是来自云南省滇南地区的一种有名特色小吃。过桥米线最早是在清朝就已经出现,距离现在至少有一百多年的历史啦!起源于建水县东城外锁龙桥西侧的鸡市街头处,有建水的特产草芽、地椒作配料,风味独特而远近闻名。深受广大美食爱好者的喜欢,不少人为此专门长途跋涉,只为尝

面霜和乳液的区别

面霜和乳液的区别
面霜和乳液的区别,相较而言乳液的水分含量要比面霜高,乳液质地要轻薄一些,乳液主要作用是保湿,滋润可以隔离外界干燥的气候,面霜既可保湿,又可美白,还能抗衰老,乳液的吸收快一些,而面霜吸收比较慢一些,因为液体的吸收速度都比较快。

怎么摘隐形眼镜

怎么摘隐形眼镜
在摘隐形眼镜之前,先用洗手液将手清洗干净,以免将细菌带入眼睛内,对着镜子,用右手中指轻拉眼睛下眼睑,左手中指轻拉眼睛上眼睑,让黑色眼球暴露在空气中,用右手食指和拇指轻触镜片的两边缘部分,向中间推使镜片拱起,再用两手指轻轻捏出镜片即可。

粉饼和散粉的区别

粉饼和散粉的区别
粉饼是呈压缩固体状态,多呈圆形或者方形,散粉则是细腻的粉末状,粉饼遮瑕力会比较强一些,可以湿用做粉底,或者用来补妆,而散粉则是定妆的效果,粉饼通常用在底妆的第一步,而散粉通常用在底妆最后一步。

冷烫和热烫的区别

冷烫和热烫的区别
冷烫和热烫的区别:冷烫对头发的要求是要在保温状态下才能给卷有个好的效果,而热烫是在干和湿的情况下都行,热烫烫出来的头发比较自然明显而有弹力,冷烫的头发风干后基本看不出来,并且发质会有点干。

高品质香水如何鉴别?用三步就可以解决

高品质香水如何鉴别?用三步就可以解决
1、看色泽以天然香料调制而成的高级香水,都有它本来的颜色,且大都是琥珀色或褐色,看起来很像宝石,比如,从茉莉、玫瑰或水仙等天然鲜花中所萃取的精油都呈黄色、褐白或绿褐色;此外,香水中所添加的魅惑香气物性香料也是褐色,苔类中的橡树苔是绿色,从树根或树根脂类中萃取的

如何去除黑眼圈

如何去除黑眼圈
去除黑眼圈首先可以用热毛巾敷眼,然后再用冷毛巾敷,十分钟就能让黑眼圈淡化的不那么严重,其次将去壳后的鸡蛋用无菌纱布包裹住,敷于眼部轻轻转动,可以急救去除黑眼圈,另外抹完眼霜后,用双手顺时针按摩,可以促进血液循环消除黑眼圈。
本文Tag