Macros

From The Document Foundation Wiki
< Macros‎ | Writer‎ | 005
Jump to: navigation, search

Coller du texte non formaté venant du presse-papiers

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 insert_Clpboard_Text_in_Writer via Outils ▸ Personnaliser ▸ [onglet]Clavier.

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

Code

REM  *****  BASIC  *****
'Copyright (c) 2011 Frieder Delor, Mailto: delorfr@googlemail.com
 
'    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
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    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 <http://www.gnu.org/licenses/>.
Option Explicit
 
sub insert_Clpboard_Text_in_Writer
dim sText As string
  sText= (getClipboardText)
  'Remplace retour à la ligne par des espaces 
  sText = Replace (sText,Chr(10)," ")
 
  'Remplace marque de paragraphe par des espaces
  sText = Replace (sText,Chr(13)," ")
  'Collage dans Writer
  Write_Cursor_Position (sText)
end sub
'--------------------------
' La procedure "Write_Cursor_Position" est inspiree de from: http://www.oooforum.org/forum/viewtopic.phtml?t=75409
' Coller du texte dans Writer a la position du curseur
Sub Write_Cursor_Position (sText as String)
Dim oViewCursor as object
dim oText As Object
  oViewCursor= thiscomponent.GetCurrentController.ViewCursor
 
  If IsEmpty(oViewCursor.Cell) Then
    oText=thiscomponent.text
  Else
    oText=oViewCursor.Cell.Text
  End If
 
  oText.insertString(oViewCursor, sText,false)
 
end Sub
 
' obtenir le texte du presse-papiers et suppression du formatage
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