How do you copy text to clipboard from macro?

I’m trying to use a macro to copy text to the clipboard from LO Base and then paste the text into Firefox.
The code below only works once. The second time I try only a few characters get pasted and then shortly after LO crashes.

Is there a bug in LO or is something wrong with my code?

I’m running under Ubuntu 18 and LO

Any help would be appreciated.

Here is the code:

GLOBAL sGlobalText as STRING

SUB CopyClipboardTitle(event as OBJECT)
  rem this routine is called when a button on the Base form is pressed to copy the text to the clipboard
  DIM oClip as OBJECT

  rem   this function makes the string and stores it in a global variable
  sGlobalText = FunctionToBuildString()                    
  oClip = createUnoService("")
  oTR = createUnoListener("TR_","")

FUNCTION TR_getTransferData(aFlavor as as Any
  IF (aFlavor.MimeType = "text/plain;charset=utf-16") THEN
    rem sGlobalText is a global variable and has already been set
    TR_getTransferData = sGlobalText

FUNCTION TR_getTransferDataFlavors() as Any
  DIM aF as NEW
  aF.MimeType = "text/plain;charset=utf-16"
  aF.HumanPresentableName = "Unicode-Text"
  TR_getTransferDataFlavors = Array(aF)

FUNCTION TR_isDataFlavorSupported(aFlavor as as BOOLEAN
  TR_isDataFlavorSupported = (aFlavor.MimeType = "text/plain;charset=utf-16")

Show the code for FunctionToBuildString

or, you can used EasyMacro:


Sub clipboard()
    app = createUnoService("net.elmau.zaz.EasyMacro")
    data = "Hello World"
End Sub


What is the purpose of this? For example to go to a specific URL? There could be a much easier method depending on this.

The code for the function “FunctionToBuildString” is rather complicated. It pulls data from several different database tables to construct a string that is stored in a global variable that should be accessible to the routines that actually copy it to the clipboard.

The purpose is to paste a text string build from records from several different database tables into an internet browser form. The string will be different every time it is copied to the clipboard.

I tried the code that mauicio provided. The line app = createUnoService(“net.elmau.zaz.EasyMacro”) results in a NULL variable “app”. And the program crashes on line app.set_clipboard(data).

Do I need to install something? Sorry I’m kind of new to LO/Ubuntu.

Maybe This post can help?

I looked at the code in that post. The only difference I saw was that the 2 functions TR_getTransferData() and TR_getTransferDataFlavors() had undefined return types whereas mine where defined as Any. I tried removing the return type. LibreOffice Base still crashed the second time I ran the macro CopyClipboardTitle() .