• Clear the clipboard (WinXP / Off2003 NL)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Clear the clipboard (WinXP / Off2003 NL)

    Author
    Topic
    #418754

    How can I clear the clipboard using (Word) VBA? I’m using these lines of code to copy text from one doc to another (based on the same template, so the sections match):

    For each sec in DocSource.Sections
    If sec.ProtectedForForms = False Then
    Set rngDocSource = Sec.Range
    rngDocSource.Copy
    Set rngDocTarget = DocTarget.Sections(sec.Index).Range
    rngDocTarget.Paste
    ‘Clear clipboard here
    End If
    Next sec

    Sometimes a section doesn’t contain any text and an error 4605 occurs. So at first, in my errorhandler I tested for this error and then proceded with Resume Next. But that means that the content of the previous section – still on the clipboard – will be pasted. Hence the need for clearing the clipboard. But I cannot find out how… confused
    I tried searching the Lounge, but all I found was the old post 255992 with the advice to use an API. But unfortunately that didn’t work. sad

    Viewing 0 reply threads
    Author
    Replies
    • #943478

      I think you can use this:

      Sub test()
      Dim oClipBoard As DataObject
      Set oClipBoard = New DataObject
      oClipBoard.Clear
      Set oClipBoard=Nothing
      End Sub

      Create a reference to the forms object lib by inserting a userform (you can remove the userform if not needed, or create the reference manually using tools, references)

      • #943631

        Thanks, Jan Karel, but no joy. sad
        I already had a userform in the project, so there is a reference to Microsoft Forms 2.0 Object Library (I checked).
        I can work around the problem with Resume NoPaste after catching error 4605 in the errorhandler, with the label NoPaste: immediately after the line rngDocTarget.Paste. But I’m still curious why nor the API EmptyClipboard, nor your solution work for me.

        • #943638
              Dim oData As New MSForms.DataObject
              With oData
                  .SetText ""
                  .PutInClipboard
              End With
              Set oData = Nothing
        • #943639

          I guess it is overkill to use Excel’s Application.CutCopyMode=False statement, right ?

    Viewing 0 reply threads
    Reply To: Reply #943631 in Clear the clipboard (WinXP / Off2003 NL)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information:




    Cancel