|
|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
模拟手工清空Office剪贴板,简单粗暴了点, - Type POINTAPI
- x As Long
- y As Long
- End Type
- Type RECT
- Left As Long
- Top As Long
- Right As Long
- Bottom As Long
- End Type
- #If VBA7 Then
- Declare PtrSafe Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWndParent As LongPtr, ByVal hWndChildAfter As LongPtr, ByVal lpszClass As String, ByVal lpszWindow As String) As LongPtr
- Declare PtrSafe Function GetWindowRect Lib "user32" (ByVal hwnd As LongPtr, lpRect As RECT) As Long
- Declare PtrSafe Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
- Declare PtrSafe Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As LongPtr)
- #Else
- Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWndParent As Long, ByVal hWndChildAfter As Long, ByVal lpszClass As String, ByVal lpszWindow As String) As Long
- Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
- Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
- Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
- #End If
- Sub ClearClipboard()
- #If VBA7 Then
- Dim hwnd As LongPtr
- #Else
- Dim hwnd As Long
- #End If
- Dim blVisible As Boolean
- Application.ScreenUpdating = True
- With Application.CommandBars("Office Clipboard")
- blVisible = .Visible
- If Not blVisible Then .Visible = True: DoEvents
- hwnd = FindClipboardWindow()
- If hwnd = 0 Then Exit Sub
- Dim RECT As RECT, pt As POINTAPI
- GetWindowRect hwnd, RECT
- ' 计算"全部清空"按钮坐标(左上角偏移)
-
- If RECT.Right - RECT.Left > 220 Then
- '"全部清空"按钮水平排列在"全部粘贴"按钮的右侧
- pt.x = RECT.Left + 140
- pt.y = RECT.Top + 20
- Else
- '"全部清空"按钮纵向排列在"全部粘贴"按钮的下方
- pt.x = RECT.Left + 62
- pt.y = RECT.Top + 53
- End If
- ' 模拟鼠标单击"全部清空"按钮
- LeftClickOnPoint pt
- .Visible = blVisible
- End With
- End Sub
- #If VBA7 Then
- Private Function FindClipboardWindow() As LongPtr
- Dim hwnd As LongPtr
- #Else
- Private Function FindClipboardWindow() As Long
- Dim hwnd As Long
- #End If
- hwnd = FindWindow("XLMAIN", ThisWorkbook.Application.Caption)
- hwnd = FindWindowEx(hwnd, 0, "EXCEL2", vbNullString)
- hwnd = FindWindowEx(hwnd, 0, "MsoCommandBar", "Office 剪贴板")
- hwnd = FindWindowEx(hwnd, 0, "MsoWorkPane", vbNullString)
- FindClipboardWindow = hwnd
- End Function
复制代码
|
|