如何将剪贴板的内容分配给openoffice BASIC宏中的数组

How to assign contents of clipboard to array in openoffice BASIC macro

我正在尝试为 LibreOffice/OpenOffice .odt 文件创建基本词汇表宏。 它将转到文档的末尾并将所选单词列表(由正则表达式找到)粘贴为唯一集(无双打)

让我失望的是,一旦文本被复制到剪贴板,我需要将内容分配给一个变量,以便我可以创建一个集合。

在OpenOffice的BASIC实现中,如何将剪贴板的内容赋值给一个新的变量?

明确一点:我不需要Paste函数,我需要在调用Paste之前将剪贴板的内容作为一个对象来操作

我正在尝试做的事情的粗略草稿是:

dispatcher.executeDispatch(document, ".uno:Copy", "", 0, Array())

rem -------------- PROBLEM IS BELOW -------
Dim oModuleUICommandDescription As Object, myText$(),aCommand
myText = thisComponent.currentSelection(0)

rem -------------- PROBLEM IS ABOVE -------

rem -------------- Followed by an array comparison to get a unique set

i = FreeFile()
Open "/path/to/my/BASIC.txt" For Output As i
Print #i, myText.string
Close #i

因此,据我所知,答案是没有简单的内置方法来执行此操作。

但是,可以使用此处发布的自定义函数(不是我的) https://wiki.documentfoundation.org/Macros/Writer/005

并使用该函数将内容分配给变量。

这里的upper sub依赖于它下面定义的函数

Sub WriteClipboardtoTxtFile()
    Dim sText As String
    Dim myTextFile As String
    Dim i%
    findAllTags_Switches()
    rem  ###########     ASSIGNMENT OCCURS JUST BELOW
    sText= (getClipboardText)
    rem ################ ASSIGNMENT OCCURS JUST ABOVE
    sText = Replace (sText," ",Chr(10))
    rem Replace white spaces with returns
    MsgBox(sText)
    i = FreeFile()
    Open "/path/to/my/file" For Output As i
    Print #i, sText
    Close #i
    
    
End Sub ' InsertClipboardTexttoVariable


Function getClipboardText() As String
    '''Returns a string of the current clipboard text'''

    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

要在OpenOffice宏编辑器中使用,将代码复制粘贴进去,即可调用新功能。