Paste Text from clipboard unformatted

From The Document Foundation Wiki
Jump to: navigation, search

Description

Here is a simple function, which allows you to paste unformatted text from the clipboard.

Just copy the function in a module, call the function from another procedure or function, for example:

sub clpboardTest
  MsgBox (getClipboardText)
end sub

Main function

Function getClipboardText () AS String

dim oClip as object ,oConverter as object
dim oClipContents as object ,oTypes as object
dim i%
oClip = createUnoService("com.sun.star.datatransfer.clipboard.SystemClipboard")
oConverter = createUnoService("com.sun.star.script.Converter")
On Error Resume Next
oClipContents = oClip.getContents
oTypes = oClipContents.getTransferDataFlavors
For i=LBound(oTypes) To UBound(oTypes)
  If oTypes(i).MimeType = "text/plain;charset=utf-16" Then
    Exit For
  End If
Next
If (i >= 0) Then
  On Error Resume Next
  getClipboardText = oConverter.convertToSimpleType _
                     (oClipContents.getTransferData(oTypes(i)), com.sun.star.uno.TypeClass.STRING)
End If
End Function