VBA to clear clipboard

Anonymous
2016-11-25T23:35:27+00:00

Does anyone know any coed that will clear the clipboard after a copy and past operation?  I've tried everything I've found through searching the web, and nothing seems to work.

Mike

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
{count} votes
Answer accepted by question author
  1. Anonymous
    2016-11-26T01:52:42+00:00

    This works fine for me.

    Private Declare Function apiOpenClipboard Lib "User32" _

                                              Alias "OpenClipboard" _

                                              (ByVal hWnd As Long) _

                                              As Long

    Private Declare Function apiEmptyClipboard Lib "User32" _

                                               Alias "EmptyClipboard" _

                                               () As Long

    Private Declare Function apiCloseClipboard Lib "User32" _

                                               Alias "CloseClipboard" _

                                               () As Long

    Function EmptyClipboard()

        If apiOpenClipboard(0&) <> 0 Then

            Call apiEmptyClipboard

            Call apiCloseClipboard

        End If

    End Function

    Edit 20161127

    Here's another alternative

    'Currently uses Late Binding so no References Req'd

    Function ClearCipboard()

    'Early binding will requires a Reference to 'Microsoft Forms 2.0 Object Library'

        Dim oData                 As Object    'New MSForms.DataObject

        Set oData = CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")

        oData.SetText Text:=Empty

        oData.PutInClipboard

        Set oData = Nothing

    End Function

    4 people found this answer helpful.
    0 comments No comments
Answer accepted by question author
  1. Anonymous
    2016-11-27T15:28:32+00:00

    I didn't even know they still had that dialog (haven't seen that since like Office 2000! wow!).  Just tested and you are right, it removes it from memory, but doesn't seem to empty the dialog itself.  I'll see if I can dig something up.

    Regarding your code, the issue is you don't have an Exit Sub after your If statement so it continues to run the your error handler no matter what to get to the End Sub.  You need to do something more like:

    Private Sub ServiceSummary_DblClick(Cancel As Integer)

        On Error GoTo Error_Handler

        If IsNull(Me.ServiceSummary) Then

            DoCmd.RunCommand acCmdPaste

        Else

            MsgBox "Cannot paste into a field that already contains data.", vbInformation, "Invalid Entry"

        End If

    Error_Handler_Exit:

        On Error Resume Next

        Exit Sub

    Error_Handler:

        MsgBox "Nothing to paste", vbInformation, "No Data"

        Call EmptyClipboard

        Resume Error_Handler_Exit

    End Sub

    Also note thought that you are setting it up that no matter what error occurs, it always reports that there is nothing to paste, yet that might not be why the error was raised in the first place.

    1 person found this answer helpful.
    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Anonymous
    2016-11-27T13:40:09+00:00

    The code seems to be working, however the window I'm referring to is below.  This is not cleared.  

    That being said, I'm having trouble with executing the following code.

    Private Sub ServiceSummary_DblClick(Cancel As Integer)

    On Error GoTo ErrorMessage_MayCauseAnError

    If IsNull(Me.ServiceSummary) Then

        DoCmd.RunCommand acCmdPaste

    Else

    MsgBox "Cannot paste into a field that already contains data.", vbInformation, "Invalid Entry"

    End If

    ErrorMessage_MayCauseAnError:

        MsgBox "Nothing to paste", vbInformation, "No Data"

    Call EmptyClipboard

    End Sub

    No matter if the field the data is being copied to, the error handler always happens.  What I want to take place is; if there is nothing on the clipboard to paste then the error handler fires, if there is something to paste and the field to be pasted to is empty (null), the data is pasted to the field, but if the field where the data is being pasted already contains data, the the "Else" message takes place.

    0 comments No comments
  2. Anonymous
    2016-11-26T13:32:04+00:00

    How do I tell if it works.  When I try it the clipboard window in Access still has items listed.

    0 comments No comments
  3. Anonymous
    2016-11-26T16:26:38+00:00

    "the clipboard window in Access"

    What clipboard window?

    Could you give us a step by step of what steps you are performing so we can replicate it and give you some suggestions.  Are you working in table/queries/forms/?  What commands are you executing?

    When I execute the EmptyClipboard command, the Paste button and CTRL+v all return nothing.

    0 comments No comments