-2

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

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

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

明确一点:我不需要粘贴功能,我需要在调用粘贴之前将剪贴板的内容作为对象操作

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

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
4

1 回答 1

0

所以,据我所知,答案是没有一种简单的内置方法可以做到这一点。

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

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

这里的上子依赖于它下面定义的函数。

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 宏编辑器中使用,请复制并粘贴代码,以便调用新函数。

于 2021-05-21T11:51:28.523 回答