How do you copy text to clipboard from macro?

asked 2019-10-14 18:15:28 +0100

george37809 gravatar image

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 6.0.7.3

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 oTR as OBJECT
  DIM oClip as OBJECT

  rem   this function makes the string and stores it in a global variable
  sGlobalText = FunctionToBuildString()                    

  oClip = createUnoService("com.sun.star.datatransfer.clipboard.SystemClipboard")
  oTR = createUnoListener("TR_","com.sun.star.datatransfer.XTransferable")
  oClip.setContents(oTR,null)
END SUB

FUNCTION TR_getTransferData(aFlavor as com.sun.star.datatransfer.DataFlavor) 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
  ENDIF
END FUNCTION

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

FUNCTION TR_isDataFlavorSupported(aFlavor as com.sun.star.datatransfer.DataFlavor) as BOOLEAN
  TR_isDataFlavorSupported = (aFlavor.MimeType = "text/plain;charset=utf-16")
END FUNCTION
edit retag flag offensive close merge delete

Comments

Show the code for FunctionToBuildString

or, you can used EasyMacro: https://gitlab.com/mauriciobaeza/zaz-...

then

Sub clipboard()
    app = createUnoService("net.elmau.zaz.EasyMacro")

    data = "Hello World"
    app.set_clipboard(data)
End Sub
mauricio gravatar imagemauricio ( 2019-10-14 18:46:00 +0100 )edit

Hello,

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

Ratslinger gravatar imageRatslinger ( 2019-10-14 22:39:17 +0100 )edit

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. Thanks.

george37809 gravatar imagegeorge37809 ( 2019-10-15 16:48:58 +0100 )edit