paste html using API

sos sos at pmg.be
Mon Mar 27 11:15:40 UTC 2023


We do it with the functions below

Function ClipboardAsText As String
Dim oClip As Variant, oClipContents As Variant, oTypes As Variant
Dim oConverter, convertedString As String
Dim i As Integer, iPlainLoc As Integer
iPlainLoc = -1
oClip = 
createUnoService("com.sun.star.datatransfer.clipboard.SystemClipboard")
oConverter = createUnoService("com.sun.star.script.Converter")
oClipContents = oClip.getContents()
oTypes = oClipContents.getTransferDataFlavors()
Dim msg As String
msg = "Sorry no usefull text in the ClipBoard found"
For i=LBound(oTypes) To UBound(oTypes)
If oTypes(i).MimeType = "text/plain;charset=utf-16" Then
iPlainLoc = i
Exit For
End If
Next i
If iPlainLoc >= 0 Then
convertedString = 
oConverter.convertToSimpleType(oClipContents.getTransferData(oTypes(iPlainLoc)), 
com.sun.star.uno.TypeClass.STRING)
ClipboardAsText=convertedString
End If
End Function

Sub CopyToClipBoard( sText )
' create SystemClipboard instance
oClip = CreateUnoService( _
"com.sun.star.datatransfer.clipboard.SystemClipboard")
oTR = createUnoListener("Tr_", _
"com.sun.star.datatransfer.XTransferable")
' set data
oClip.setContents(oTR,Null)
sTxtCString = sText
'oClip.flushClipboard() ' does not work
End Sub

Function Tr_getTransferData(aFlavor as com.sun.star.datatransfer.DataFlavor)
' wordt pas aangeroepen als we cntrol+V doen intss is sTxtCString reeds 
gezet
If (aFlavor.MimeType = "text/plain;charset=utf-16") Then
Tr_getTransferData() = sTxtCString
End If
End Function

Function Tr_getTransferDataFlavors()
Dim aFlavor As new com.sun.star.datatransfer.DataFlavor
aFlavor.MimeType = "text/plain;charset=utf-16"
aFlavor.HumanPresentableName = "Unicode-Text"
Tr_getTransferDataFlavors() = array(aFlavor)
End Function

Function Tr_isDataFlavorSupported( _
aFlavor as com.sun.star.datatransfer.DataFlavor) as Boolean
If aFlavor.MimeType = "text/plain;charset=utf-16" Then
Tr_isDataFlavorSupported = true
Else
Tr_isDataFlavorSupported = false
End If
End Function

hope it helps

Fernand

On 26/03/2023 22:26, Zorg wrote:
>
> Thanks
>
> Ok  it seem a goot way
>
> maybe it is obvious but how can i paste the clipboard in my odt 
> without using dispatch
>
>
>
> Le 26/03/2023 à 21:20, Caolán McNamara a écrit :
>> On Sun, 2023-03-26 at 12:06 +0200, Zorg wrote:
>>> ... We have try using XTransferable but without any success.
>> I think it should be possible in headless mode to use the clipboard
>> apis to copy and paste within LibreOffice. But in this case the
>> clipboard is basically a fake clipboard only for the headless
>> libreoffice instance, one which doesn't interact with any real system
>> clipboard.
>>
>> So for example in your current code I see that copy_with_format copies
>> into a true X clipboard via xclip so in headless more we can't paste
>> from there and it has to be non headless to get that to work.
>>
>> But if the LibreOffice apis to put something into the clipboard are
>> used then I think it should work to "Paste" within LibreOffice and get
>> that content back out, even in headless more.
>>
>> f.e. in this example at
>> https://ask.libreoffice.org/t/is-there-any-way-to-copy-calc-cell-content-and-paste-something-else/32173/11
>> there is:
>>
>> oClip =
>> ctx.getServiceManager().createInstanceWithContext("com.sun.star.datatra
>> nsfer.clipboard.SystemClipboard", ctx)
>> oClip.setContents(transferable, None)
>>
>> where "transferable" is implemented in that example as something that
>> only supports "text/plain;charset=utf-16" so if you change
>> copy_with_format to something that follow the model of the above
>> example implementation of an XTransferable to set a transferable that
>> provides the data and its mimetype as text/html I would expect it to
>> work.
>>


More information about the LibreOffice mailing list