Results 1 to 8 of 8
  1. #1
    4 Star Lounger
    Join Date
    Dec 2003
    Location
    Zoetermeer, Zuid-Holland, Netherlands
    Posts
    559
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Clear the clipboard (WinXP / Off2003 NL)

    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... <img src=/S/confused.gif border=0 alt=confused width=15 height=20>
    I tried searching the Lounge, but all I found was the old <post#=255992>post 255992</post#> with the advice to use an API. But unfortunately that didn't work. <img src=/S/sad.gif border=0 alt=sad width=15 height=15>

  2. #2
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Clear the clipboard (WinXP / Off2003 NL)

    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)
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  3. #3
    4 Star Lounger
    Join Date
    Dec 2003
    Location
    Zoetermeer, Zuid-Holland, Netherlands
    Posts
    559
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Clear the clipboard (WinXP / Off2003 NL)

    Thanks, Jan Karel, but no joy. <img src=/S/sad.gif border=0 alt=sad width=15 height=15>
    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.

  4. #4
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Clear the clipboard (WinXP / Off2003 NL)

    <pre> Dim oData As New MSForms.DataObject
    With oData
    .SetText ""
    .PutInClipboard
    End With
    Set oData = Nothing</pre>


  5. #5
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Clear the clipboard (WinXP / Off2003 NL)

    I guess it is overkill to use Excel's Application.CutCopyMode=False statement, right <g>?
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  6. #6
    4 Star Lounger
    Join Date
    Dec 2003
    Location
    Zoetermeer, Zuid-Holland, Netherlands
    Posts
    559
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Clear the clipboard (WinXP / Off2003 NL)

    Thanks. I'll giva that a try.

  7. #7
    4 Star Lounger
    Join Date
    Dec 2003
    Location
    Zoetermeer, Zuid-Holland, Netherlands
    Posts
    559
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Clear the clipboard (WinXP / Off2003 NL)

    In one Word: yeah...

  8. #8
    4 Star Lounger
    Join Date
    Dec 2003
    Location
    Zoetermeer, Zuid-Holland, Netherlands
    Posts
    559
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Clear the clipboard (WinXP / Off2003 NL)

    It works! <img src=/S/yep.gif border=0 alt=yep width=15 height=15>
    Thanks again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •