PutInClipboard Office 365

gallen

Well-known Member
Joined
Jun 27, 2011
Messages
2,016
My company has just upgraded to 365.

My previous version was Office 2013

I had code that copied the text from a selected cell to the clipboard. This now doesn't work in 365

The code is as below:

Code:
Public Sub AddToClipboard(s As String)
   
Dim oCopy As DataObject


        Set oCopy = New DataObject
        oCopy.SetText s
        oCopy.PutInClipboard
    
End Sub

This has worked fine until 365 was installed.

I tried late binding using similar code I found from Google:

Code:
Sub CopyText(Text As String)
    Dim o As Object
    Set o = CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
    o.SetText Text
    o.PutInClipboard
    Set o = Nothing
End Sub

This has the same result. When I paste anything I've copied it shows "??"

I've debugged line by line and the correct text is being added in the 'SetText' line but pasting gives me the 2 question marks.

Any clues?
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
It's actually nothing to do with Office. There's a bug in Windows 8 and later that causes this to happen if you have File Explorer windows open. You should use Windows API calls to put text in the clipboard to be safe.
 
Upvote 0
ahhh! Of course. I forgot to mention they upgraded to Win 10 from Win7. Many thanks
 
Upvote 0
Another clipboard tool that might work :
Code:
Function SetClipBoardText(ByVal Text As Variant) As Boolean
    SetClipBoardText = CreateObject("htmlfile").ParentWindow.ClipboardData.SetData("Text", Text)
End Function

Function GetClipBoardText() As String
    On Error Resume Next
    GetClipBoardText = CreateObject("htmlfile").ParentWindow.ClipboardData.GetData("Text")
End Function

Function ClearClipBoardText() As Boolean
    ClearClipBoardText = CreateObject("htmlfile").ParentWindow.ClipboardData.clearData("Text")
End Function


Code:
Sub Test()
    SetClipBoardText "Hello"
    MsgBox GetClipBoardText
    ClearClipBoardText
    MsgBox GetClipBoardText
End Sub
 
Upvote 0
Another clipboard tool that might work :
Code:
Function SetClipBoardText(ByVal Text As Variant) As Boolean
    SetClipBoardText = CreateObject("htmlfile").ParentWindow.ClipboardData.SetData("Text", Text)
End Function

Function GetClipBoardText() As String
    On Error Resume Next
    GetClipBoardText = CreateObject("htmlfile").ParentWindow.ClipboardData.GetData("Text")
End Function

Function ClearClipBoardText() As Boolean
    ClearClipBoardText = CreateObject("htmlfile").ParentWindow.ClipboardData.clearData("Text")
End Function


Code:
Sub Test()
    SetClipBoardText "Hello"
    MsgBox GetClipBoardText
    ClearClipBoardText
    MsgBox GetClipBoardText
End Sub

This works a treat. Nice workaround.... Thank you
 
Upvote 0

Forum statistics

Threads
1,214,400
Messages
6,119,288
Members
448,885
Latest member
LokiSonic

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top