String in Zwischenablage kopieren

Programmierung unter AOO/LO (StarBasic, Python, Java, ...)

Moderator: Moderatoren

Carolyn
Beiträge: 4
Registriert: Mo, 21.11.2005 00:52

String in Zwischenablage kopieren

Beitrag von Carolyn »

Hallo,
ich suche nach einer Möglichkeit einen String aus einem Makro in die Zwischenablage zu kopieren.

Mein Ansatz den String in eine Zelle zu schreiben und diese dann via copyrange in die Zwischenablage zu nehmen ist untauglich, weil da der cursor zwangsweise zu der Zelle springt und ich das nicht gebrauchen kann.

Vielen Dank schonmal
Caro
Paule
****
Beiträge: 113
Registriert: Do, 22.09.2005 16:26
Wohnort: Schleswig

Beitrag von Paule »

Wieso willst du das?

Der cursor Springt bei mir nicht auf die Position.

benutz du den Makrorecorder?

Poste doch deinen Code den kann man dir besser helfen


Paule
Toxitom
********
Beiträge: 3769
Registriert: Di, 12.08.2003 18:07
Wohnort: Wiesbaden
Kontaktdaten:

Beitrag von Toxitom »

Hey Caro,
ich suche nach einer Möglichkeit einen String aus einem Makro in die Zwischenablage zu kopieren.
Das ist nicht ganz einfach. Zwar bietet die API diverse Möglichkeiten, doch da musst du gaaaaaaanz tief eintauchen.

Eine Lösung findest du im englischen Forum, und zwar genau -> hier, in dem Post musst du suchen nach UtilAPI Module (falls du nicht direkt dorthin gelangst. Die wichtigsten Code-Teile hab ich dir mal dort rauskopiert. Das Startmakro macht nichts anderes, als den String zu übergeben.
Na ja, die Methode, ein leeres Writer-Dokument zu erzeugen, ist sicher nicht absolut elegant, funktioniert aber :-)

Code: Alles auswählen

sub Test
	dim sText as String
	sText = "mein Text für die Zwischenablage"
	TextToClipboard(sText)
end sub


Sub TextToClipboard( ByVal cText As String )
	Dim arg(0) as new com.sun.star.beans.PropertyValue
	arg(0).name = "Hidden"
	arg(0).value = true
   oDoc = StarDesktop.loadComponentFromURL( "private:factory/swriter", "_blank", 0, arg())
   
   ' Get the text of the document.
   oText = oDoc.getText()
   ' Get a cursor that can move over or to any part of the text.
   oCursor = oText.createTextCursor()
   
   ' Insert text and paragraph breaks into the text, at the cursor position.
   oText.insertString( oCursor, cText, False )
   
   SelectAll( oDoc )
   ClipboardCopy( oDoc )
   
   oDoc.close( True )
End Sub 

Sub ClipboardCopy( oDocumentFrame )
   DocumentDispatch( oDocumentFrame, ".uno:Copy" )
'   DocumentDispatch( oDocumentFrame, "slot:5711" )
End Sub 

Sub SelectAll( oDocumentFrame )
   DocumentDispatch( oDocumentFrame, ".uno:SelectAll" )
'   DocumentDispatch( oDocumentFrame, "slot:5723" )
End Sub 

Sub DocumentDispatch( ByVal oDocumentFrame As Object,_
                  ByVal cURL As String,_
                  Optional cTargetFrameName,_
                  Optional nSearchFlags,_
                  Optional aDispatchArgs )
   
   ' If they gave us the wrong parameter...
   If Not IsNull( oDocumentFrame ) Then
      If Not HasUnoInterfaces( oDocumentFrame, "com.sun.star.frame.XFrame" ) Then
         ' Be sure that we've got the document frame.
         ' Someone might have passed us the document model or one of
         '  its controller's.
         oDocumentFrame = GetDocumentFrame( oDocumentFrame )
      EndIf
   EndIf
   
   If IsMissing( cTargetFrameName ) Then
      cTargetFrameName = ""
   EndIf
   If IsMissing( nSearchFlags ) Then
      nSearchFlags = 0
   EndIf
   If IsMissing( aDispatchArgs ) Then
      aDispatchArgs = Array()
   EndIf
   
   oDispatchHelper = createUnoService( "com.sun.star.frame.DispatchHelper" )
   oDispatchHelper.executeDispatch( oDocumentFrame, cURL, cTargetFrameName, nSearchFlags, aDispatchArgs )
End Sub

'----------
' This will always return the document's frame.
' Pass in any one of...
'   * the document's model (subclass of com.sun.star.document.OfficeDocument)
'   * the document's controller
'   * the document's frame
Function GetDocumentFrame( oDoc As Object ) As Object
   Dim oFrame As Object
   
   ' If the caller gave us the document model...
   If oDoc.supportsService( "com.sun.star.document.OfficeDocument" ) Then
      ' ...then get the controller from that.
      oCtrl = oDoc.getCurrentController()
      ' ...then get the frame from the controller.
      oFrame = oCtrl.getFrame()

   ' If the caller gave us a document controller...
   ElseIf HasUnoInterfaces( oDoc, "com.sun.star.frame.XController" ) Then
      oCtrl = oDoc
      ' ...then get the frame from the controller.
      oFrame = oCtrl.getFrame()
   
   ' If the caller gave us the document frame...
   ElseIf HasUnoInterfaces( oDoc, "com.sun.star.frame.XFrame" ) Then
      ' ...thanks!  That's just what we wanted!
      oFrame = oDoc
   
   Else
      ' The caller did not give us what we expected!
      MsgBox( "GetDocumentFrame called with incorrect parameter." )
   EndIf
   
   GetDocumentFrame() = oFrame
End Function
Viel Spass und bis denn,

Thomas
Unterstützer LibreOffice, zertifizierter Trainer und Berater
Bücher: LibreOffice 6- Einstieg und Umstieg
Makros Grundlagen - LibreOffice / OpenOffice Basic
Carolyn
Beiträge: 4
Registriert: Mo, 21.11.2005 00:52

Beitrag von Carolyn »

erstmal Danke für eure Bemühungen ... leider komm ich heute nicht mehr ausgiebig zum testen, werde das aber morgen nachholen.
Paule hat geschrieben:Wieso willst du das?
das ganze wird/ist eine Art Calc-Kassenbuch in der die erste Zeile die Namen enthält und die zweite Zeile die jeweiligen Guthaben. Das Makro soll aus diesen Zellen einen Text erstellen, der -BBverCodet- formatiert in ein Forum gepastet werden kann:
[font=courier]Summe Hans Julia Otto[/font]
[font=courier]25 12,23 8,31 4,46[/font]

Das Schlimme an der Sache ist, das ich schon eine fix und fertige perfekt funktionierende Lösung hatte ... bis ... ja bis zu dem Tag an dem das OO112 alle meine Makros ins Nirvana geschickt hat und ich begriff, wo OO Makros speichert :( und das das Backup meiner Dokumente reichlich sinnfei war.
Paule hat geschrieben:Poste doch deinen Code den kann man dir besser helfen
nich haun ;) ... das hat noch keine Funktion und ist nur ein Test für das o.g. Problem:

Sub Uebersicht_fuer_Forum

dim document as object
dim dispatcher as object
dim Post as String

myDoc = thisComponent
mySheet = myDoc.Sheets().getByName("Kasse")

Post = "lala"

myCell = mySheet.getCellByPosition(0,0)
myCell.string = Post

myDoc.CurrentController.Select(mycell)

'msgbox(myCell.string)

document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dispatcher.executeDispatch(document, ".uno:Copy", "", 0, Array())

end sub


@Toxitom
deine Lösung gefällt mir richtig gut (obwohl es für ein simples "Copy" ziemlicher overkill ist ;) - aber das ist ja schon fast normal).... ich werde da morgen mal dran basteln und mich wieder melden.
Antworten