Colle le contenu du presse-papiers comme texte non formaté après avoir supprimé les retours à la ligne et les mises en forme

From The Document Foundation Wiki
< Macros‎ | Writer
Jump to: navigation, search
This page is a translated version of the page Macros/Writer/005 and the translation is 100% complete.

Other languages:
English • ‎français


Cette macro colle le contenu du presse-papiers comme texte non formaté, après avoir supprimé les retours à la ligne et les mises en forme. Pour faciliter son utilisation vous pouvez associer un raccourci-clavier à la procédure InsertClipboardTextInWriter via Outils ▸ Personnaliser ▸ [onglet]Clavier.

PS: pour simplement coller du texte non formaté vous pouvez utiliser la macro générale getClipboardText().


' Copyright (c) 2011 Frieder Delor, Mailto:
'    This program is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 3 of the License, or
'    (at your option) any later version.
'    This program is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    GNU General Public License for more details.
'    You should have received a copy of the GNU General Public License
'    along with this program.  If not, see <>.

Option Explicit

Sub InsertClipboardTextInWriter()
    Dim sText As String
    sText= (getClipboardText)
    sText = Replace (sText,Chr(10)," ") ' Remplace retour à la ligne par des espaces
    sText = Replace (sText,Chr(13)," ") ' Remplace marque de paragraphe par des espaces
    WriteCursorPosition(sText) ' Collage dans Writer
End Sub ' InsertClipboardTextInWriter

Sub WriteCursorPosition(sText as String)
    '''Cette procedure est inspiree de:'''
    '''Coller du texte dans Writer a la position du curseur'''

    Dim oViewCursor As Object '
    Dim oText As Object
    oViewCursor = ThisComponent.GetCurrentController.ViewCursor
    If IsEmpty(oViewCursor.Cell) Then
        oText = ThisComponent.text
        oText = oViewCursor.Cell.Text
    End If
    oText.insertString(oViewCursor, sText, false)

End Sub ' WriteCursorPosition

Function getClipboardText() As String
    '''Renvoie une "String" du texte du presse-papiers courant'''

    Dim oClip As Object '
    Dim oConverter As Object '
    Dim oClipContents As Object
    Dim oTypes As Object
    Dim i%

    oClip = createUnoService("")
    oConverter = createUnoService("")
    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

    If (i >= 0) Then
        On Error Resume Next
        getClipboardText = oConverter.convertToSimpleType _
    End If

End Function ' getClipboardText