Office-Zwischenablage mit VBA löschen

    • VBA: Excel

      Office-Zwischenablage mit VBA löschen

      Hallo liebe Community,

      es gibt ja eine Office-Zwischenablage, in der die kopierte Informationen abgelegt werden.

      Jetzt gibt es bei mir(nicht mehr) täglich mehrmals eine Fehlermeldung die heißt 'Kann die Zwischenablage nicht leeren', unabhängig davon, ob in der Office-Zwischenablage etwas drin ist oder nicht.
      Das nervt unheimlich, wenn Sie ein Makro, das Kopieren-Einfügen -Funktionen beinhaltet, im Hintergrund laufen lassen haben und das jedes Mal sein Dienst quittiert, weil die Office-Zwischenablage angeblich voll ist.
      Meine Recherchen im Internet brachten mich nicht so weit, um das Problem mit VBA zu lösen. Nun habe ich doch im Internet ein VBA-Makro gefunden, das für Excel 2003-2007 geschrieben war.
      Den Code habe ich modifiziert und angepasst sodass unter Excel 2010 (deutsche Version) 32bit und 64bit laufen kann.
      Wer Interesse hat oder probieren will, kann den Quelcode kopieren und in einem allgemeinen Modul einfügen.

      Vie Spaß damit

      Spoiler anzeigen

      Visual Basic-Quellcode

      1. 'Code
      2. Option Explicit
      3. #If VBA7 And Win64 Then
      4. Private Declare PtrSafe Function FindWindowEx Lib "USER32" _
      5. Alias "FindWindowExA" (ByVal hWnd1 As LongPtr, ByVal hWnd2 As LongPtr, _
      6. ByVal lpsz1 As String, ByVal lpsz2 As String) As LongPtr
      7. Private Declare PtrSafe Function PostMessage Lib "USER32" Alias "PostMessageA" _
      8. (ByVal hwnd As LongPtr, ByVal wMsg As Long, _
      9. ByVal wParam As LongPtr, ByVal lParam As LongPtr) As Long
      10. Private Declare PtrSafe Function apiOpenClipboard Lib _
      11. "USER32" Alias "OpenClipboard" (ByVal hwnd As LongPtr) As Long
      12. Private Declare PtrSafe Function apiEmptyClipboard Lib _
      13. "USER32" Alias "EmptyClipboard" () As Long
      14. Private Declare PtrSafe Function apiCloseClipboard Lib _
      15. "USER32" Alias "CloseClipboard" () As Long
      16. Private Declare PtrSafe Sub Sleep Lib _
      17. "kernel32.dll" (ByVal dwMilliseconds As Long)
      18. #Else
      19. Private Declare Function apiOpenClipboard Lib _
      20. "USER32" Alias "OpenClipboard" (ByVal hwnd As Long) As Long
      21. Private Declare Function apiEmptyClipboard Lib _
      22. "USER32" Alias "EmptyClipboard" () As Long
      23. Private Declare Function apiCloseClipboard Lib _
      24. "USER32" Alias "CloseClipboard" () As Long
      25. Private Declare Sub Sleep Lib _
      26. "kernel32.dll" (ByVal dwMilliseconds As Long)
      27. Private Declare Function FindWindowEx Lib _
      28. "user32.dll" Alias "FindWindowExA" (ByVal hWnd1 As Long, _
      29. ByVal hWnd2 As Long, ByVal lpsz1 As String, _
      30. ByVal lpsz2 As String) As Long
      31. Private Declare Function PostMessage Lib _
      32. "user32.dll" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
      33. ByVal wParam As Long, ByVal lParam As Long) As Long
      34. #End If
      35. Private Const WM_LBUTTONDOWN As Long = &H201&
      36. Private Const WM_LBUTTONUP As Long = &H202&
      37. Private Function MakeLong(ByVal nLoWord As Integer, ByVal nHiWord As Integer) As Long
      38. MakeLong = nHiWord * 65536 + nLoWord
      39. End Function
      40. Sub ClearOfficeClipboard()
      41. #If VBA7 And Win64 Then
      42. Dim hMain As LongPtr, hExcel2 As LongPtr, hClip As LongPtr, hWindow As LongPtr, hParent As LongPtr
      43. Dim lParameter As Long, sTask$
      44. #Else
      45. Dim hMain As Long, hExcel2 As Long, hClip As Long, hWindow As Long, hParent As Long
      46. Dim lParameter As Long, sTask$
      47. #End If
      48. Dim ocbShow As Boolean
      49. sTask = Application.CommandBars("Office Clipboard").NameLocal
      50. ocbShow = Application.CommandBars("Office Clipboard").Visible
      51. 'Office-Zwischenablage muss sichtbar sein
      52. If Not Application.CommandBars("Office Clipboard").Visible = True Then
      53. Application.CommandBars("Office Clipboard").Visible = True
      54. End If
      55. Application.ActiveWindow.Activate
      56. Application.ScreenUpdating = False
      57. Application.ScreenUpdating = True
      58. Sleep (1000)
      59. ' Handle for XLMAIN
      60. hMain = Application.hwnd
      61. 'finden OfficeClipboard Fenster
      62. '2 Methoden, da wir nicht sicher sind, ob es sichtbar ist
      63. 'Sobald es sichtbar gemacht wird, wird WindowClass erstellt
      64. 'Und bleibt für die Dauer der Instanz geladen
      65. Do
      66. hExcel2 = FindWindowEx(hMain, hExcel2, "EXCEL2", vbNullString)
      67. hParent = hExcel2: hWindow = 0
      68. hWindow = FindWindowEx(hParent, 0, "MsoCommandBar", sTask)
      69. If hWindow Then
      70. hParent = hWindow: hWindow = 0
      71. hWindow = FindWindowEx(hParent, 0, "MsoWorkPane", vbNullString)
      72. If hWindow Then
      73. hParent = hWindow: hWindow = 0
      74. hClip = FindWindowEx(hParent, 0, "bosa_sdm_XL9", vbNullString)
      75. If hClip > 0 Then
      76. Exit Do
      77. End If
      78. End If
      79. End If
      80. Loop While hExcel2 > 0
      81. If hClip = 0 Then
      82. hParent = hMain: hWindow = 0
      83. hWindow = FindWindowEx(hParent, 0, "MsoWorkPane", vbNullString)
      84. If hWindow Then
      85. hParent = hWindow: hWindow = 0
      86. hClip = FindWindowEx(hParent, 0, "bosa_sdm_XL9", vbNullString)
      87. End If
      88. End If
      89. If hClip = 0 Then
      90. Call ClipWindowForce
      91. hParent = hMain: hWindow = 0
      92. hWindow = FindWindowEx(hParent, hWindow, "MsoWorkPane", vbNullString)
      93. If hWindow Then
      94. hParent = hWindow: hWindow = 0
      95. hClip = FindWindowEx(hParent, hWindow, "bosa_sdm_XL9", vbNullString)
      96. End If
      97. End If
      98. If hClip = 0 Then
      99. MsgBox "Kann das Fenster von Zwischwnablage nicht finden."
      100. Application.CommandBars("Office Clipboard").Visible = ocbShow
      101. Application.ScreenUpdating = True
      102. Exit Sub
      103. End If
      104. lParameter = MakeLong(120, 18)
      105. Call PostMessage(hClip, WM_LBUTTONDOWN, 0&, lParameter)
      106. Call PostMessage(hClip, WM_LBUTTONUP, 0&, lParameter)
      107. DoEvents
      108. 'Ursprung wiederherstellen
      109. Application.CommandBars("Office Clipboard").Visible = ocbShow
      110. Application.ScreenUpdating = True
      111. MsgBox "Office-Zwischenablage entleert!"
      112. End Sub
      113. '?????? bis jetzt brauchte ich das nicht
      114. Sub ClipWindowForce()
      115. Dim octl
      116. With Application.CommandBars("Office Clipboard")
      117. If Not .Visible Then
      118. Application.ScreenUpdating = False
      119. Set octl = Application.CommandBars(1).FindControl(ID:=809, recursive:=True)
      120. If Not octl Is Nothing Then octl.Execute
      121. '.Visible = False
      122. Application.ScreenUpdating = True
      123. End If
      124. End With
      125. End Sub
      126. Sub mach_endlich_mit_der_schei_schluss()
      127. Call ClearOfficeClipboard
      128. 'Call apiOpenClipboard(0) 'Windows-Zwischenablage
      129. 'apiEmptyClipboard 'Windows-Zwischenablage
      130. 'apiCloseClipboard 'Windows-Zwischenablage
      131. Application.CutCopyMode = False
      132. End Sub


      Edit by hal2000:
      - Expander und Code-Tags eingefügt.

      Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „hal2000“ ()