Just for fun with graphics

Yes. The mechanism for copying to the clipboard is the same as when copying an image from any LibreOffice module.
As for copying Graphic to/from a file, GraphicProvider handles that.

Maybe I expressed it a little unclear:
When you copy image data to the clipboard using your test project (ClipBoardTest3.odt), the DataFlavors are added to the package. But the package does not contain a filename because it doesn’t even exisist. But even if was existing you cant add it to DataFlavors because LibreOffice Basic is not able to to write that value in UTF-16 format ( application/x-openoffice-file;windows_formatname=“FileNameW”)

If you want, you can test it by running your own test project and then my test project:
getTransferDataFlavors.odt (14.8 KB)

I once wrote a macro that (for later study) saves the clipboard contents in all formats to C:\Temp\ClipBoard directory.

Option Explicit

' Writes the contents of the clipboard (all formats) to files C:\temp\Clipboard\Clip_ ...
Sub ClipboardToFiles()
  Dim oClip As Object, oContents As Object, oTransferDataFlavors As Object, aDataFlavor As Object
  Dim oFA As Object, oFile As Object, oTextStream As Object
  Dim aData() As Byte, i As Long, DFName As String, fileName as String, folder as String, v
  
  folder = "C:\Temp\Clipboard"  ' ???
  
  oFA=CreateUnoService("com.sun.star.ucb.SimpleFileAccess") 
  oContents = CreateUnoService("com.sun.star.datatransfer.clipboard.SystemClipboard").getContents()
  If oContents Is Nothing Then 
    Exit Sub
  End If
  
  oTransferDataFlavors = oContents.getTransferDataFlavors()
  oFA=CreateUnoService("com.sun.star.ucb.SimpleFileAccess") 
  On Error Resume Next
  fileName=folder & "\Clip_" & Format(Now(), "YYYYMMDD\_HHMMSS")
  For i=0 To Ubound(oTransferDataFlavors)
    aDataFlavor = oTransferDataFlavors(i)
    DFName=aDataFlavor.HumanPresentableName
    oFile=oFA.openFileWrite(ConvertToUrl(fileName & "_" & Replace(DFName, " ", "_")))
    aData=Array()      
    v=Empty
    v=oContents.getTransferData(aDataFlavor)
    ' mri oContents 
    If IsArray(v) Then
      aData=oContents.getTransferData(aDataFlavor) 
      oFile.writeBytes aData
    ElseIf Not IsEmpty(v) Then
      oTextStream = CreateUnoService("com.sun.star.io.TextOutputStream")
      oTextStream.setOutputStream oFile
      oTextStream.writeString oContents.getTransferData(aDataFlavor) 
      oFile.writeBytes aData
    End If  
  Next i  
End Sub

I used my macro to see what happens when we copy file(s) to the clipboard using Explorer (this is all well-known).
Three DataFlavors are created:

  1. FileList: a list of full file paths (UTF-16 Little Endian encoding)
  2. FileContent: the contents of the (first) file (binary file)
  3. FileName: the name of the (first) file

When I tested your test project
ClipBoardTest3.odt (57.7 KB) (mouse right click → Paste option stays grayed)
And then examined content of the clipboard to see what DataFlavors looks like, using my own test project…
getTransferDataFlavors.odt (14.8 KB)
the result looked like this:
Näyttökuva 2025-10-29 205848
Näyttökuva 2025-10-29 205904
Näyttökuva 2025-10-29 205917
Näyttökuva 2025-10-29 205927
Näyttökuva 2025-10-29 205944
Näyttökuva 2025-10-29 205957
Näyttökuva 2025-10-29 210009
Näyttökuva 2025-10-29 210020
Näyttökuva 2025-10-29 210030
Näyttökuva 2025-10-29 210040
When I copy one image file to the clipboard by right-clicking and selecting Paste and then examine content of the clipboard to see what DataFlavors looks like, using my own test project… the result looks like this:
Näyttökuva 2025-10-29 215147
Näyttökuva 2025-10-29 215204
Näyttökuva 2025-10-29 215219

' The filename can be extracted (UTF-8 format) like this: 
If oTypes(i).MimeType Like "*FileNameW*"  Then
        ImgPath = oClipContents.getTransferData(oTypes(i))	
       'oImg.Model.ImageUrl = ConvertToUrl(ImgPath) ' of course you could use this but that's not the idea at this time
       ext = "." & Split(oClipContents.getTransferData(oTypes(i)), ".")(UBound(Split(oClipContents.getTransferData(oTypes(i)), ".")))
End If
1 Like

Please have you some documentation or description for operator Like? I see it in code for 1st time and it seems functional, but I don’t know in what all cases

Sub fceLike
	dim s$
	s="abc"
	msgbox s like "ab*" 'show True
	msgbox s like "*bc" 'show True
End Sub

152689 – BASIC: Like operator (available in compatibility mode) needs documentation :wink:

1 Like

Bug for operator Like
https://bugs.documentfoundation.org/show_bug.cgi?id=169147

2 Likes

yep.
not an easy spot …

We can convert FileList into an array of strings, but to do this we’ll have to write a 2-line function in Python (which can be called from Basic).

@ sokol92:

I wrote a .NET ComVisble .dll to handle the whole thing, which can be used in Basic with the CreateObject method.

Unfortunately the code contains more than two lines, because of a few Imports lines. Public Sub …End Sub takes already those two lines…

But as an additional feature, drag and drop functionality has been introduced.

To avoid leaving any mystery to future readers of this topic, I’ll quote these two lines:

def arr_decode(arg, encoding = "utf-8"):
    return bytearray(arg).decode(encoding)

By the way, I remembered a trick for LO Basic (and VBA too).
To assign a UTF16-encoded byte array to a String variable…

you can...

you can use the assignment operator.

This way, we can see a list of files selected by Windows Explorer:

' ...
    Dim aByte() as Byte, arr, j as Long
    
    If aDataFlavor.HumanPresentableName = "FileList" Then
      arr = oContents.getTransferData(aDataFlavor)
      Redim aByte(Ubound(arr))
      For j = 0 To UBound(arr) Step 2
        aByte(j+1) = arr(j+1)
        aByte(j) = IIf(arr(j) = 0 And arr(j+1) = 0, 10, arr(j))  ' Replace Chr(0) -> Chr(10)
      Next j  
      Msgbox CStr(aByte)
    End If
1 Like

Well, it looks like this has turned into really fun :upside_down_face:

sokol92

Unfortunately your code snippet does not work…

Sub GetDataFromClipBoard()

    Dim oClip As Object, aDataFlavours As Object, oClipContents As Object
    Dim aBytes() As Byte, oData As Object, i%, j%
	
    oClip = createUnoService("com.sun.star.datatransfer.clipboard.SystemClipboard")
    If IsNull(oClip.getContents) Or IsEmpty(oClip.getContents)  Then Exit Sub
    oClipContents = oClip.getContents()
    aDataFlavours = oClipContents.getTransferDataFlavors

    For i = LBound(aDataFlavours) To UBound(aDataFlavours)    
        If aDataFlavours(i).MimeType Like "*FileList*"  Then
            oData = oClipContents.getTransferData(aDataFlavours(i))
            If IsArray(oData) Then
                Redim aBytes(Ubound(oData))
                For j = 0 To UBound(oData) Step 2
                    aBytes(j+1) = oData(j+1)
                    On Error Resume Next
                    aBytes(j) = IIf(oData(j) = 0 And oData(j+1) = 0, 10, oData(j))
                    If Err <> 0 Then Err.Clear
                Next j
                MsgBox CStr(aBytes) ' does not show scandinavian charachters
            End If
        End If
    Next  i
End Sub

Yes, interestingly, it displays Cyrillic letters correctly for me.
Let’s figure it out and find the culprit!

It didn’t take long to find the culprit.

aBytes(j) = IIf(oData(j) < 0, 256 + oData(j), IIf(oData(j) = 0 And oData(j+1) = 0, 10, oData(j)))

2025-11-01 143858

1 Like

sokol92

It works perfectly!

Näyttökuva 2025-11-01 134831
:wink:

1 Like

I’m done with this thing.
PoorMan’s ClipBoardDrop.odt (22.2 KB)

1 Like

@sokol92 that can’t be a correct code to handle UTF-16! Sorry, it can!

Finally, I found a way to copy the image data from an image object in a dialog box to the clipboard. But unfortunately I cannot paste the image data elsewhere. For example, save as a file from the clipboard to the desktop.

In this case transferring the imagedata via clipboard is only possible within LibreOffice, for example from a Writer project to another Writer project. The reason for this is that LibreOffice cannot save the filename in UTF-16 format, which the clipboard needs for understanding what is being done with the stored data when right clicking and selecting paste.

Assuming you are interested you can download the test projects from the links below…
ClipBoardTest.odt (26.0 KB)
2ndClipBoardTest.odt (21.4 KB)