Page 1 of 1

How can I put something INTO the clipboard?

Posted: Thu Jan 01, 2009 11:54 pm
by adempf
It's quite easy to put something, say text, from the clipboard to the cursorposition. But how can I do the opposite: Put some given text into the clipboard? I tried:

sub main
zwspeichersetzen( "some text" )
texteinfuegen
end sub

sub ZwSpeichersetzen (text$)
rem define variables
dim odocument as object
dim odispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
odocument = ThisComponent.CurrentController.Frame
odispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
dim args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "Text"
args1(0).Value = text$
odispatcher.executeDispatch(odocument, ".uno:Copy", "", 0, args1())
end sub


sub Texteinfuegen
rem define variables
dim odoc as object
dim odis as object
rem ----------------------------------------------------------------------
rem get access to the document
odoc = ThisComponent.CurrentController.Frame
odis = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
odis.executeDispatch(odoc, ".uno:Paste", "", 0, Array())
end sub

The sub Texteinfuegen (=paste) works fine, the other part does not. More exactly: Texteinfuegen pastes the last input of the clipboard, Zwspeichersetzen apparently does nothing at all. Why?

Or more important: How can I change that?

Re: How can I put something INTO the clipboard?

Posted: Fri Jan 02, 2009 5:35 am
by FJCC
Andrew Pitonyak has a document with many macro examples at this link http://www.pitonyak.org/oo.php I think you want the document labeled English Macro Document. One section (5.23.4) is called "Storing a String to the Clipboard". There is also a German translation, but it is much older. I haven't used this particular code, so I can't give any more help than pointing you to the document. I hope that helps.

Re: How can I put something INTO the clipboard?

Posted: Tue Jan 20, 2009 12:11 am
by adempf
Thank you FJCC,

this seems to be the right place to look for my solution.

Unfortunately it is not working (that is: not at all) - error messages without an end! Every time I try to correct one I open a new problem somewhere else. And worst of all: I have no idea how this code shall do anything at all - where is the string that is put into the clipboard, for instance?

The last version I tried reads:

Sub Main
Dim null As Object
Dim sClipName As String
Dim oClip As Object
Dim oClipContents as Any
sClipName = "com.sun.star.datatransfer.clipboard.SystemClipboard"
oClip = createUnoService(sClipName)
oTRX = createUnoListener("TR_", "com.sun.star.datatransfer.XTransferable")
oClipContents = oClip.setContents(oTRX, null)
End Sub

Function TR_getTransferData( aFlavor As com.sun.star.datatransfer.DataFlavor ) As Any
If (aFlavor.MimeType = "text/plain;charset=utf16") Then TR_getTransferData = "From OO with love ..."
End Function

Function TR_getTransferDataFlavors() As Any
Dim aF As New com.sun.star.datatransfer.DataFlavor
aF.MimeType = "text/plain;charset=utf16"
aF.HumanPresentableName = "UnicodeText"
TR_getTransferDataFlavors = Array(aF)
End Function

Function TR_isDataFlavorSupported( aFlavor As com.sun.star.datatransfer.DataFlavor ) As Boolean
'My XP system beep shows that this routine is called every 2 seconds
'call MyPlaySoundSystem("SystemAsterisk", true)
TR_isDataFlavorSupported = (aFlavor.MimeType = "text/plain;charset=utf16")
End Function

This does not produce another flood of errors - and the code definitely does something: The clipboard is empty when I execute the Main-Routine.

That is not exactly what I was looking for :) - but it leaves room for new hope ...

Is there anybody who could help?

Re: How can I put something INTO the clipboard?

Posted: Tue Jan 20, 2009 4:21 am
by FJCC
I grabbed this code out of A. Pitonyak's document and it transfers the string "Test line" to the clipboard. In his document the line is "From OOo with love..." You can see that I even accidentally left the page number 74 in the code! The only differences I see between his code and yours is the "Private oTRX" and you don't have an "end if" in Function TR_getTransferData. I do not understand how this code works. I tried following it by stepping through the with the debug tools and it just baffled me. But, hey, it works!

Code: Select all

Private oTRX
Sub Main
Dim null As Object
Dim sClipName As String
sClipName = "com.sun.star.datatransfer.clipboard.SystemClipboard"
oClip = createUnoService(sClipName)
oTRX = createUnoListener("TR_", "com.sun.star.datatransfer.XTransferable")
oClipContents = oClip.setContents(oTRX, null)
End Sub
Function TR_getTransferData( aFlavor As com.sun.star.datatransfer.DataFlavor ) As Any
If (aFlavor.MimeType = "text/plain;charset=utf-16") Then
TR_getTransferData = "Test line"
EndIf
End Function
Function TR_getTransferDataFlavors() As Any
Dim aF As New com.sun.star.datatransfer.DataFlavor
aF.MimeType = "text/plain;charset=utf-16"
aF.HumanPresentableName = "Unicode-Text"
TR_getTransferDataFlavors = Array(aF)
End Function
Function TR_isDataFlavorSupported( aFlavor As com.sun.star.datatransfer.DataFlavor ) As Boolean
'My XP system beep - shows that this routine is called every 2 seconds
'call MyPlaySoundSystem("SystemAsterisk", true)
74
TR_isDataFlavorSupported = (aFlavor.MimeType = "text/plain;charset=utf-16")
End Function

Re: How can I put something INTO the clipboard?

Posted: Wed Jan 21, 2009 7:59 pm
by adempf
FJCC wrote:I grabbed this code out of A. Pitonyak's document and it transfers the string "Test line" to the clipboard. In his document the line is "From OOo with love..." You can see that I even accidentally left the page number 74 in the code! The only differences I see between his code and yours is the "Private oTRX" and you don't have an "end if" in Function TR_getTransferData. I do not understand how this code works. I tried following it by stepping through the with the debug tools and it just baffled me. But, hey, it works!
Thank you again, FJCC. I just worked out where the error messages came from: I had a first line "Option Explicit" that did cause the messages.

Nevertheless I think I have to get a better understanding to do what I wanted: It works just fine with a fix String like "Test line" or "From OOo with love..." - but until now I did not work out how to try it with a String that is stored in a variable - at least not with a global one that is set in a different sub / function ...

But may be I will find a way - I promise I will post the solution (if there is one and I can find it).

Re: How can I put something INTO the clipboard?

Posted: Wed Jan 21, 2009 9:47 pm
by JohnV
Because ".uno:copy" normally copies the text selected by the view cursor we can use a variation of a recorded copy macro to store a variable to the clipboard.

Code: Select all

Sub CopyVariableToClipboard
dim document   as object
dim dispatcher as object
dim oDoc, oVC, Mark, var
oDoc = ThisComponent
oVC = oDoc.CurrentController.getViewCursor
Mark = oDoc.Text.createTextCursorByRange(oVC)
oVC.collapseToEnd
var = "My text"
oVC.String = var
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dispatcher.executeDispatch(document, ".uno:Copy", "", 0, Array())
oVC.String = ""
oVC.gotoRange(Mark,false)
End Sub

Re: How can I put something INTO the clipboard?

Posted: Wed Jan 21, 2009 11:09 pm
by adempf
JohnV wrote:Because ".uno:copy" normally copies the text selected by the view cursor we can use a variation of a recorded copy macro to store a variable to the clipboard.

Sub CopyVariableToClipboard
dim document as object
dim dispatcher as object
dim oDoc, oVC, Mark, var
oDoc = ThisComponent
oVC = oDoc.CurrentController.getViewCursor
Mark = oDoc.Text.createTextCursorByRange(oVC) 'This line causes problems!
oVC.collapseToEnd
var = "My text"
oVC.String = var
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dispatcher.executeDispatch(document, ".uno:Copy", "", 0, Array())
oVC.String = ""
oVC.gotoRange(Mark,false)
End Sub

Thank you, JohnV, but the line I marked in red causes the error message (translated from German error message, so don't be too picky about the exact words):
BASIC runtime error
Exception occured
Type: com.sun.star.uno.RuntimeException
Message: .

Re: How can I put something INTO the clipboard?

Posted: Wed Jan 21, 2009 11:25 pm
by JohnV
Make sure the cursor isn't in a table or frame.

You can run the macro without that line and the last line but your cursor may not be where you left it when you finish.

Re: How can I put something INTO the clipboard?

Posted: Wed Jan 21, 2009 11:28 pm
by adempf
Something I can't understand:

I renamed the sub Main to sub settext, put a new (empty) Main in front, defined a global variable var$, and set a value "My text" in the function TR_getTransferData.
Everything worked fine: the variable was passed into the clipboard and could be pasted afterwards.

After that I tried to set the Variable anywhere else - and it failed. A global variable should be global, shouldn't it? That usually should mean, that you can set it wherever you like and use it wherever you like it.

But what settext does after that change is: empty the clipboard.

This version works (you just use settext):

Code: Select all

Sub settext
Dim null As Object
Dim sClipName As String
sClipName = "com.sun.star.datatransfer.clipboard.SystemClipboard"
oClip = createUnoService(sClipName)
oTRX = createUnoListener("TR_", "com.sun.star.datatransfer.XTransferable")
oClipContents = oClip.setContents(oTRX, null)
End Sub

Function TR_getTransferData( aFlavor As com.sun.star.datatransfer.DataFlavor ) As Any
var$="My text"
If (aFlavor.MimeType = "text/plain;charset=utf-16") Then
TR_getTransferData = var$
EndIf
This one doesn't:

Code: Select all

Sub settext
Dim null As Object
Dim sClipName As String
var$="My text"
sClipName = "com.sun.star.datatransfer.clipboard.SystemClipboard"
oClip = createUnoService(sClipName)
oTRX = createUnoListener("TR_", "com.sun.star.datatransfer.XTransferable")
oClipContents = oClip.setContents(oTRX, null)
End Sub

Function TR_getTransferData( aFlavor As com.sun.star.datatransfer.DataFlavor ) As Any
If (aFlavor.MimeType = "text/plain;charset=utf-16") Then
TR_getTransferData = var$
EndIf

Re: How can I put something INTO the clipboard?

Posted: Thu Jan 22, 2009 12:02 am
by adempf
JohnV wrote:Make sure the cursor isn't in a table or frame.

You can run the macro without that line and the last line but your cursor may not be where you left it when you finish.
O.k., the macro runs fine as long as I had some text marked. For my real purpose I can simply say: That is never the case. I wanted to use the macro to put the name of a picture as a subtitle under the picture. I have a macro that helps me to find the picture in my folders, select the right picture, put it's name (without extension) into a variable say name$, paste the picture into the place where the cursor was before - and every text that was marked is overwritten by the picture. Because of this your macro does not work for me. Thank you nevertheless.

Re: How can I put something INTO the clipboard?

Posted: Fri Jan 30, 2009 11:29 am
by Anser
Can the below given code/functions be modified to copy a Tab Delimited text (which is already available in the ClipBoard) to a Particular cell/range. Right now when I try to execute the Uno Paste, but a Dialog named Text Import is appearing and the user has to manually click the Ok button on that dialog to continue with the paste. Is there any way to avoid the Text Import Dialof from appearing.

I have been searching and seeking help to solve this issue for more than 25 days and till today I am not successful

A Sample data available on the clipboard is . I have used the word (Tab) to show where the tab charactrer is. Just try create similiar data on any text editor and copy it to the clipboard. Now Open calc and try to use the paste. Definitely a Dialog will appear "Text Import" and will ask you for the confirmation. I am trying to avoid this dialog

Samle Data

Code: Select all

Mr.xxxxx (Tab)  20000
Mr.yyyyyy (Tab) 30000
Mr.zzzzzz (Tab) 40000

Code: Select all

Private oTRX
Sub Main
   Dim null As Object
   Dim sClipName As String
   sClipName = "com.sun.star.datatransfer.clipboard.SystemClipboard"
   oClip = createUnoService(sClipName)
   oTRX = createUnoListener("TR_", "com.sun.star.datatransfer.XTransferable")
   oClipContents = oClip.setContents(oTRX, null)
End Sub

'*------------------------------------------------------------------------------------------------------------*
Function TR_getTransferData( aFlavor As com.sun.star.datatransfer.DataFlavor ) As Any
'*------------------------------------------------------------------------------------------------------------*
If (aFlavor.MimeType = "text/plain;charset=utf-16") Then
   TR_getTransferData = "Test line"
EndIf
End Function

'*------------------------------------------------------------------------------------------------------------*
Function TR_getTransferDataFlavors() As Any
'*------------------------------------------------------------------------------------------------------------*
Dim aF As New com.sun.star.datatransfer.DataFlavor
aF.MimeType = "text/plain;charset=utf-16"
aF.HumanPresentableName = "Unicode-Text"
TR_getTransferDataFlavors = Array(aF)
End Function

'*------------------------------------------------------------------------------------------------------------*
Function TR_isDataFlavorSupported( aFlavor As com.sun.star.datatransfer.DataFlavor ) As Boolean
'*------------------------------------------------------------------------------------------------------------*
TR_isDataFlavorSupported = (aFlavor.MimeType = "text/plain;charset=utf-16")
End Function
Regards

Anser

Re: How can I put something INTO the clipboard?

Posted: Tue Jan 05, 2010 1:48 am
by sdaau
Just wanted to share my 2 cents on this.

I was simpy trying to make some code, that will loop through all fields in a document in OOO Writer (dev 3.2, XP), record some of their parameters in a report string, and toss that string into the clipboard, ready to be pasted elsewhere.

It seems, regardless of how the idea of copying via setContents seems cool, that it's impossible to solve copying the content, of a arbitrary string variable, to clipboard - using only setContents in Basic (in writer). The reason for that is probably what is said in Common Application Features - OpenOffice.org Wiki:
http://wiki.services.openoffice.org/wiki/Documentation/DevGuide/OfficeDev/Common_Application_Features wrote:Note: The architecture of the OpenOffice.org clipboard service is strongly conforming to the Java clipboard specification....
To copy to the clipboard, implement a transferable object that supports the interface com.sun.star.datatransfer.XTransferable. The transferable object offers arbitrary formats described by DataFlavors.
The following Java example demonstrates the implementation of a transferable object. This transferable object contains only one format, unicode text.
...
public class TextTransferable implements XTransferable ...
i.e. we would have to "strongly conform" to Java specification from Basic (which from my noob perspective, would be kind of difficult to do?!).

Whereas the code works fine if a string is written directly in TR_getTransferData - the biggest problem seems to me, that regardless of what type of global variable is used to pass a value to TR_getTransferData, that variable "expires" by the time TR_getTransferData fires (and it fires whenever you press Paste in any other program, after you've ran the macro once).

I'm leaving below my code with comments below - more less as documentation of what failed to work during these hours I've fiddled with it. What I ended up using, is the code in OpenOffice.org Forum :: Copy arbitrary string (text) to Clipboard - which basically uses Copy and Paste in a hidden document (instead of setContents), to allow for a proper setting of content of Clipboard.

Cheers!

Code: Select all

REM  *****  BASIC  *****

'' Andrew's Macro Information - http://documentation.openoffice.org/HOW_TO/various_topics/AndrewMacro.odt
Private oTRX
Private const UNICODE_CONTENT_TYPE as String = "text/plain;charset=utf-16" 

''' "Dim" here means same as "Public" - a "public domain" variable: 
''' "These variables are available to all of the modules in their library"

''Dim gStr 
''Private gStr ''Dim gStr as String 

Global gStr As String '' doesn't matter if Global, tried also: '' Private gStr As String  
Private oDoc '' doesn't matter if it is here or not

Sub Main
  Dim null as Object
  Dim s$  ''alternative/shorthand declaration as string
  Dim oClip
  Dim oClipContents
  
  gStr = "From OO with love ... 2"

  s = "com.sun.star.datatransfer.clipboard.SystemClipboard"
  oClip = createUnoService(s)

  s = "com.sun.star.datatransfer.XTransferable"
  oTRX = createUnoListener("TR_", s)
  oClipContents = oClip.setContents(oTRX, null)
  ''msgbox(gStr) '' right, if this is kept on screen, then the paste is succesful;
  '' if OK is pressed on msgbox so it dissapears, paste will have gStr as empty string
  '' Global gStr shouldn't lose scope, but it does. 
  '' However if there is one paste with msgbox on screen, then OK on msgbox so it dissapears, then 
  ''   subsequent pastes will work
  
  '' trying to fake with a document instead of msgbox as above: 
  '' as per Copy arbitrary string (text) to Clipboard - http://www.oooforum.org/forum/viewtopic.phtml?t=6065
  '' seems to work: 
  '' * only if there is breakpoint on getTransferDataFlavors and getTransferData !! ; or:
  '' * if there are msgbox in getTransferDataFlavors and getTransferData
  '' but still - that works only on first run, subsequent paste gStr is empty
  dim odis as object
  odis = createUnoService("com.sun.star.frame.DispatchHelper")
  oDoc = StarDesktop.loadComponentFromURL( "private:factory/swriter", "_blank", 0,_
      Array( MakePropertyValue( "Hidden", True ) ) )    
  ' Get the text body of the document.
  oText = oDoc.getText()
  ' Get a cursor that can move over or to any part of the text.
  oCursor = oText.createTextCursor() 
  
  odis.executeDispatch(oDoc.CurrentController.Frame, ".uno:Paste", "", 0, Array())
  oText = oDoc.getText()
  msgbox("MN " + oText.String)
  ''oDoc.close( True ) '' doesn't matter if oDoc is Private, local or if we close it or not
End Sub 

'' by the time this fires, gStr has probably lost scope
Function TR_getTransferData( aFlavor As com.sun.star.datatransfer.DataFlavor ) As Any

  msgbox("gTD " + gStr)
  If (aFlavor.MimeType = UNICODE_CONTENT_TYPE) Then
    TR_getTransferData = gStr
  EndIf
End Function
 
Function TR_getTransferDataFlavors() As Any
  Dim aF as new com.sun.star.datatransfer.DataFlavor
  aF.MimeType =    UNICODE_CONTENT_TYPE
  aF.HumanPresentableName = "Unicode-Text"
  TR_getTransferDataFlavors = Array(aF)
  msgbox("gTDF " + gStr)
End Function
 
Function TR_isDataFlavorSupported( aFlavor As com.sun.star.datatransfer.DataFlavor ) As Boolean

  Dim s$
  'My XP system beeps - shows that this is called every 2 seconds
  'call MyPlaySoundSystem("SystemAsterisk", true) 
  s = UNICODE_CONTENT_TYPE
  TR_isDataFlavorSupported = (aFlavor.MimeType = s)
End Function 

Re: How can I put something INTO the clipboard?

Posted: Sat Feb 06, 2010 4:22 pm
by ms777
Anser wrote:Can the below given code/functions be modified to copy a Tab Delimited text (which is already available in the ClipBoard) to a Particular cell/range. Right now when I try to execute the Uno Paste, but a Dialog named Text Import is appearing and the user has to manually click the Ok button on that dialog to continue with the paste. Is there any way to avoid the Text Import Dialof from appearing.

...

Anser
Hi,

you can catch and process unwanted pop up windows by the technique shown in http://www.oooforum.org/forum/viewtopic.phtml?t=22845 . First example could be a base for your issue ...

Good luck,

ms777