Clipboard RTF Data on Windows

OK, this is driving me batty now. How can I set RTF data on the Windows Clipboard?

You need WindowsClipboardMBS.RegisterClipboardFormat to get RTF code and then put it on the clipboard with that type.

I read though the documentation and the example in the RegisterClipboardFormat

Var theFormat As Integer = WindowsClipboardMBS.RegisterClipboardFormat("Rich Text Format")
Var isAvaialble As Boolean = WindowsClipboardMBS.IsClipboardFormatAvailable(theFormat)

This returns a negative value for theFormat, isAvailable is false, and trying to setData returns false.

A simple search of the line above returned tons of entries; notably:

Clipboard Formats
and

Clipboard

and it looks like that is the right way to do it.
There isn’t a “getLastError” on the MBS Plugin, so I’m at a loss for how to fix it - tried a bunch of variations but no good.

Here is a snippet of code from one of our apps.

clipboardObj = New Clipboard

#If TargetMacOS Then
  clipboardObj.AddRawData(rtfData, "RTF ")
#Else
  clipboardObj.AddRawData(rtfData, "Rich Text Format")
#EndIf

clipboardObj.Close

Note. We also set the Text property to a non-styled version of the text before we add the raw data. I am unsure if you would need to do that to enable the adding of raw data.

clipboardObj = New Clipboard
clipboardObj.Text = plaintText

#If TargetMacOS Then
  clipboardObj.AddRawData(rtfData, "RTF ")
#Else
  clipboardObj.AddRawData(rtfData, "Rich Text Format")
#EndIf

clipboardObj.Close
2 Likes

Thank you.
I’ve implemented that and I can paste the text version but I cannot get the RTF to paste in anywhere.
But… It does paste the unformatted text, so it doesn’t break anything.

Are you sure your RTF is valid?

I am. It comes from a text area and this works perfectly on Mac.
When I was iterating the possible solutions the best I could do was get it to copy the RTF data as plain text, and I validated that it was right on Win as well.
Of course, I could be missing something simple.

Well, have you tried to copy some RTF in another app and then inspect the clipboard?
e.g. by enumerating what is in the clipboard?

Yep, and Rich Text Format shows up.

When I call this I get -28
Var id As Integer= WindowsClipboardMBS.RegisterClipboardFormat(“Rich Text Format”)

then when I cal ClipBoardFormats it shows 28 (not negative)

I’ve tried doing the SetData with the returned value and it’s abs, but still no joy.
When I call GetClipBoardFormatName it comes back blank.

I don’t think this is me. Is it possible there is a bug?

2 Likes

Sorry, you found a bug in the plugin. I made a mess with the data types and caused an integer overflow :frowning:
I’ll fix the plugin.

2 Likes

That’s great to know.
Thanks, Christian!

New plugin here:
https://www.dropbox.com/scl/fo/714hvwzr2rqjp3bukepuk/AHWokqR5QaB62WEHiik-FSo?rlkey=ufslamodl084gj844uuq7wgfl&dl=0

To enumerate formats:

// show list of all formats
var w as new WindowsClipboardMBS

var formats() as string
var f as integer = w.EnumClipboardFormats(0)
while f <> 0
  formats.Add f.ToString+": "+w.GetClipboardFormatName(f)
  
  f = w.EnumClipboardFormats(f)
wend

MessageBox join(formats, EndOfLine)

get the RTF data:

Var TypeRTF as Integer = WindowsClipboardMBS.RegisterClipboardFormat("Rich Text Format")
var w as new WindowsClipboardMBS

if w.IsClipboardFormatAvailable(TypeRTF) then
  var rtf as string = w.GetData(TypeRTF)
  
  Break
  
end if

Seems to work fine now.

4 Likes

That’s awesome.
Thank you very much.

1 Like