Incollare dagli appunti un testo non formattato

    From The Document Foundation Wiki
    This page is a translated version of the page Macros/General/008 and the translation is 100% complete.


    Other languages:

    Descrizione

    Questa macro permette di incollare dagli appunti del testo non formattato.

    Codice

    In LibreOffice Basic:

    Option Explicit
    
    Sub ClipboardTest
        MsgBox getClipboardText
    End Sub ' ClipboardTest
    
    Function getClipboardText() As String
        '''Restituisce una stringa corrispondente al testo copiato negli appunti'''
    
        Dim oClip As Object ' com.sun.star.datatransfer.clipboard.SystemClipboard
        Dim oConverter As Object ' com.sun.star.script.Converter
        Dim oClipContents As Object
        Dim 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 ' getClipboardText

    ODT file to test macro