由于在Office 2003版中,VBA不直接提供操作Office剪贴板的方法,因此只能借助API函数来完成。 主要思路: ①:先载入剪贴板窗口 ②:然后找到剪贴板的窗口句柄 ③:向剪贴板窗口发送鼠标按下和弹起的消息,模拟按下“全部清空” 这儿,以Word为例,其它Office程序(如Excel、PowerPoint)可以如法炮制。 '************************************************************************* '作者:Joe Was '功能:清空Office剪贴板 '整理:ExcelHome论坛 '测试环境:Word 2003 '相关链接:http://www.mrexcel.com/forum/showthread.php?t=167292 '*************************************************************************
'声明API函数 Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long '定义鼠标消息 Private Const WM_LBUTTONDOWN As Long = &H201& Private Const WM_LBUTTONUP As Long = &H202& Sub ClearClipBoard() Dim hWord, hClipBoard, hWindow As Long 'Word主窗口、剪贴板、临时窗口句柄 Dim nXY As Long '剪贴板“全部清空”按钮的坐标 Dim strCaption As String 'Word主窗口的标题 Application.ShowClipboard '打开剪贴板 CommandBars("Task Pane").Visible = False '关闭任务窗格 '先获取Word主窗口的标题 If Documents.Count = 0 Then '没有文档打开 strCaption = "Microsoft Word" Else strCaption = ActiveWindow.Caption & " - Microsoft Word" End If hWord = FindWindowEx(0&, 0&, "OpusApp", strCaption) '获取Word主窗口句柄 hWindow = FindWindowEx(hWord, 0&, "MsoWorkPane", "MsoWorkPane") '获取任务窗格窗口句柄 hClipBoard = FindWindowEx(hWindow, 0&, "bosa_sdm_Microsoft Office Word 11.0", "Collect and Paste 2.0") '获取剪贴板窗口句柄 If hClipBoard = 0 Then '未知原因,没有找到剪贴板的窗口句柄 MsgBox "剪贴板清空失败!" Exit Sub End If nXY = 100 + 10 * 65536 '(x,y)=(100,10),取(92,6)~(168,27)均可 Call PostMessage(hClipBoard, WM_LBUTTONDOWN, 0&, nXY) '模拟鼠标按下 Call PostMessage(hClipBoard, WM_LBUTTONUP, 0&, nXY) '模拟鼠标弹起 End Sub
[此贴子已经被作者于2008-3-27 11:24:25编辑过] |