我在 Windows 10 64 位下使用 Office 365。我正在尝试清除剪贴板。宏录制器产生一个空的子。
以下尝试大部分收集自如何使用VBA清除Office剪贴板:
Option Explicit
Public Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function EmptyClipboard Lib "user32" () As Long
Public Declare Function CloseClipboard Lib "user32" () As Long
Public Sub ClearClipboardA()
Application.CutCopyMode = False
End Sub
Public Sub ClearClipBoardB()
' Source: http://www.vbaexpress.com/kb/getarticle.php?kb_id=462
Dim oData As New DataObject
oData.SetText Text:=Empty ' Clear
oData.PutInClipboard ' Putting empty text into the clipboard to empty it
End Sub
Public Sub ClearClipboardC()
OpenClipboard (0&)
EmptyClipboard
CloseClipboard
End Sub
版本A:
未找到方法或数据成员
版本 B:运行时不清除剪贴板。 很快就会出现一个黄色的小状态窗口:
“7 of 24 - 剪贴板 | 未获取元素”
(翻译成英文)
版本C:似乎什么也没发生。
在上面的参考用户 iamstrained 中写道:“...如果有人正在寻找如何在 64 位下的 Office 365 中执行此操作,您现在需要使用向后兼容性的修改来实现此操作:
Private Declare PtrSafe
和 LongPtr
因为您对这些值的两次更改将解决问题并使其仍然有效。”
我找到了对 Microsoft 页面的引用,其中可能已经完成了此操作:
使用此处显示的 subs,我可以将文本插入剪贴板并从中提取,但不能清除它。
清除办公室剪贴板(来自 Excel):
#If VBA7 Then
Private Declare PtrSafe Function AccessibleChildren Lib "oleacc" (ByVal paccContainer As Office.IAccessible, _
ByVal iChildStart As Long, ByVal cChildren As Long, _
ByRef rgvarChildren As Any, ByRef pcObtained As Long) As Long
Public Const myVBA7 As Long = 1
#Else
Private Declare Function AccessibleChildren Lib "oleacc" (ByVal paccContainer As Office.IAccessible, _
ByVal iChildStart As Long, ByVal cChildren As Long, _
ByRef rgvarChildren As Any, ByRef pcObtained As Long) As Long
Public Const myVBA7 As Long = 0
#End If
Public Sub EvRClearOfficeClipBoard()
Dim cmnB, IsVis As Boolean, j As Long, Arr As Variant
Arr = Array(4, 7, 2, 0) '4 and 2 for 32 bit, 7 and 0 for 64 bit
Set cmnB = Application.CommandBars("Office Clipboard")
IsVis = cmnB.Visible
If Not IsVis Then
cmnB.Visible = True
DoEvents
End If
For j = 1 To Arr(0 + myVBA7)
AccessibleChildren cmnB, Choose(j, 0, 3, 0, 3, 0, 3, 1), 1, cmnB, 1
Next
cmnB.accDoDefaultAction CLng(Arr(2 + myVBA7))
Application.CommandBars("Office Clipboard").Visible = IsVis
End Sub
我可以确认下面的代码会清除Windows剪贴板
#If Win64 Then
Declare PtrSafe Function OpenClipboard Lib "user32" (ByVal hwnd As LongPtr) As Long
Declare PtrSafe Function CloseClipboard Lib "user32" () As Long
Declare PtrSafe Function EmptyClipboard Lib "user32" () As Long
#Else
Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Declare Function CloseClipboard Lib "user32" () As Long
Declare Function EmptyClipboard Lib "user32" () As Long
#End If
Public Sub ClearClipboard()
OpenClipboard (0&)
EmptyClipboard
CloseClipboard
End Sub
您可以从 https://www.microsoft.com/en-us/download/confirmation.aspx?id=9970
下载指针安全声明我已经使用了上面的代码片段,它运行良好,直到最近的软件更新阻止我在不打开剪贴板窗口的情况下清除办公室剪贴板。我的解决方案非常简单 - 将其添加到代码中:
#If VBA7 Then
Private Declare PtrSafe Function AccessibleChildren Lib "oleacc" (ByVal paccContainer As Office.IAccessible, _
ByVal iChildStart As Long, ByVal cChildren As Long, _
ByRef rgvarChildren As Any, ByRef pcObtained As Long) As Long
Public Const myVBA7 As Long = 1
#Else
Private Declare Function AccessibleChildren Lib "oleacc" (ByVal paccContainer As Office.IAccessible, _
ByVal iChildStart As Long, ByVal cChildren As Long, _
ByRef rgvarChildren As Any, ByRef pcObtained As Long) As Long
Public Const myVBA7 As Long = 0
#End If
Public Sub EvRClearOfficeClipBoard()
Dim cmnB, IsVis As Boolean, j As Long, Arr As Variant
Arr = Array(4, 7, 2, 0) '4 and 2 for 32 bit, 7 and 0 for 64 bit
Set cmnB = Application.CommandBars("Office Clipboard")
'Just add here...First
'---------------------
With Application
.DisplayClipboardWindow = True
End With
IsVis = cmnB.Visible
If Not IsVis Then
cmnB.Visible = True
DoEvents
End If
For j = 1 To Arr(0 + myVBA7)
AccessibleChildren cmnB, Choose(j, 0, 3, 0, 3, 0, 3, 1), 1, cmnB, 1
Next
cmnB.accDoDefaultAction CLng(Arr(2 + myVBA7))
Application.CommandBars("Office Clipboard").Visible = IsVis
'And finish with this
'--------------------
With Application
.DisplayClipboardWindow = False
End With
End Sub
EvR 清除Office 剪贴板的宏非常聪明。 (参见上文。)它可以在 Excel 中与我的 64 位 Microsoft (Office) 365 和 Windows 10 一起使用。
VBA7 随 Office 2010 一起引入;现在每个人都应该拥有它。 vba7指令不区分64位;如果需要的话,win64 指令可以做到这一点。这是我的 EvR 宏版本:
Declare PtrSafe Function AccessibleChildren Lib "oleacc" ( _
ByVal paccContainer As Office.IAccessible, _
ByVal iChildStart As Long, ByVal cChildren As Long, _
ByRef rgvarChildren As Any, ByRef pcObtained As Long) As Long
Sub ClearOfficeClipboard()
Dim A() As Variant, CB As Variant, n As Variant, i As Integer
With Application
If Not .DisplayClipboardWindow Then
ClearClipboard
.DisplayClipboardWindow = True
ClearOfficeClipboard ' recurse
Exit Sub
End If
On Error GoTo ErrHandler
Set CB = .CommandBars("Office Clipboard")
A = Array(0, 3, 0, 3, 0, 3, 1)
For i = 0 To UBound(A)
AccessibleChildren CB, A(i), 1, CB, n
Next i
CB.accDoDefaultAction CLng(0)
End With
ErrHandler:: Set CB = Nothing
End Sub
ClearClipboard 宏来自 Chip Pearson,http://www.cpearson.com/Excel/Clipboard.aspx。 (参见 Timothy Rylatt 上文,但用 vba7 代替 win64。)
我不明白调用 AccessibleChildren 的 For 循环。有人可以解释它是如何工作的吗?请注意,CB 必须是 Variant,而不是 CommandBar 或 Object。
我不太清楚这里关于不同 Office 版本以及整个 VBA7 和 Bit 内容的讨论。评论里好像有一些不同的解释。
但是根据最近对这里和其他地方的代码的一些审查,我认为我们在这里所做的是清除 Office 的剪贴板查看器。具体来说,我们使用 VBA 执行的操作与我们单击该按钮(在 English Office 中)
Clear All
按钮时手动执行的操作相同。
https://www.excelfox.com/forum/showthread.php/2824-Tests-Copying-pasting-Cliipboard-issues?p=18037&viewfull=1#post18037
(在我的德国 Office 版本中,按钮是Alle löschen
)。我认为,到目前为止,这里讨论的编码是使用一些 COM 包装器接口事物进行某种 GUI 层次结构导航,这使我们能够获得可以宽松地描述为“活动可访问性”的东西。这是我外行理解的极限。
“活动”位可能解释了为什么“查看者痛苦”需要开放才能使编码工作。 可访问按钮的层次结构可能在 Office 2016 左右发生了变化,因此对于不同 Office 版本的代码更改,需要考虑的主要问题可能是 Office 版本。 (另外一件需要考虑的小事情是早期代码部分中使用的文字字符串,用于检查 Viewer Pain 是否打开,因为我认为从 Office 2007 开始,这一点发生了变化)
我认为下面的编码版本可以完成 Office 2003 及以上版本的工作。我已经在几台装有 Office 版本 2003 2007 2010 2013 的计算机上对其进行了彻底测试。我只有一个更高版本,Office 2016,并且它可以工作。我知道其他几个人发现它可以在他们的 Office 365 中运行。也许如果任何路过的人发现它在他们的办公室中是否运行,那么他们可以发表评论并让我们知道,并请提供他们的 Office 版本,谢谢。
当前对此解决方案和其他解决方案的讨论正在此处进行
https://eileenslounge.com/viewtopic.php?p=321817#p321817
https://www.eileenslounge.com/viewtopic.php?p=321822#p321822
Option Explicit
#If VBA7 Then
Declare PtrSafe Function AccessibleChildren Lib "oleacc" (ByVal paccContainer As Office.IAccessible, ByVal iChildStart As Long, ByVal cChildren As Long, ByRef rgvarChildren As Any, ByRef pcObtained As Long) As Long
#Else
Declare Function AccessibleChildren Lib "oleacc" (ByVal paccContainer As Office.IAccessible, ByVal iChildStart As Long, ByVal cChildren As Long, ByRef rgvarChildren As Any, ByRef pcObtained As Long) As Long
#End If
Sub small_20202024_ClearOfficeClipBoard_() ' https://eileenslounge.com/viewtopic.php?p=319159&sid=a5636ddee2213f0629c9f46423c324c5#p319159
Dim avAcc, bClipboard As Boolean, j As Long
Dim MyPain As String
If CLng(Val(Application.Version)) <= 11 Then ' Case 11: "Excel 2003" Windows "Excel 2004" mac
Let MyPain = "Task Pane"
Else
Let MyPain = "Office Clipboard"
End If
Set avAcc = Application.CommandBars(MyPain) '
Let bClipboard = avAcc.Visible ' bClipboard will be false if the viewer pain is not open
If Not bClipboard Then
avAcc.Visible = True ' This opens the Viewer pain. The coding won't work if it is not open
DoEvents: DoEvents
Else
End If
' coding change for Office versions at -- Office 2016 ==
If CLng(Val(Application.Version)) < 16 Then
' --For Office versions 2003 2007 2010 2013 ----------------------------------------
For j = 1 To 4 ' J = 1 2 3 4
AccessibleChildren avAcc, Choose(j, 0, 3, 0, 3), 1, avAcc, 1
Next
avAcc.accDoDefaultAction 2& ' This seems to do the clearing It will NOT error if viewer pain is already Cleared 1& for paste
' ----------------------------------------------------------------------------------
Else
' ==For Office versions 2016 and higher ==============================================
For j = 1 To 7 ' J = 1 2 3 4 5 6 7
AccessibleChildren avAcc, Choose(j, 0, 3, 0, 3, 0, 3, 1), 1, avAcc, 1
Next
avAcc.accDoDefaultAction 0& ' This seems to do the clearing It WILL error if viewer pain is already Cleared
End If ' =======================================================================
Let Application.CommandBars(MyPain).Visible = bClipboard ' Puts the viewer pain back as it was, open or closed
End Sub
艾伦