Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Macro that launches a Google search of the string selected in LibreOffice


About a year ago, I asked for help on concerning a macro that launches a Google search of the string that is currently selected in LibreOffice. A poster called echo8hink was so kind to give me the code for the macro, namely the following:

sub Google_search
    Dim oSelections, oFirstSelection As Object
    Dim sPhrase, sGooglePhrase,sSplitArray(), sSendPhrase As String
    oSelections = ThisComponent.CurrentSelection
    oFirstSelection = oSelections.getByIndex(0 )
    sPhrase = oFirstSelection.String        'assign selected text to string variable
    If sPhrase = "" Then
        MsgBox "Please select text first."
        Exit Sub
        'work on string, replace space with +
        sSplitArray() = split(sPhrase," ")
        sSendPhrase = Join(sSplitArray(),"+")   
        'build Google string
        sGooglePhrase = ""+sSendPhrase+"%22"
        'launch browser, full screen window, with Google string
    End If
end sub

Now, I have had an idea for a little improvement of the macro, but I do not know how to implement it myself because I do not know how to write a macro. Since does not exist any more, I cannot ask for help there, and so I would like to ask for it here.

The idea for the improvement is the following: What the macro currently does is that it takes the string that is selected in LibreOffice and launches the Internet browser to search for the string (which is first wrapped in quotation marks) on Google. (The macro as given above works on Ubuntu with Chromium installed. For other systems or other browsers, the command "chromium-browser" has to be replaced by the command that launches the intended browser on the given system.) The macro is currently intended for cases where the search string consists of several words. (That's why the string is wrapped in quotation marks.) Sometimes, though, I do not want to search for a string that consists of several words, but only for a single word. And I am thinking that, in such cases, it should not be necessary to select the word, but that it should be sufficient to indicate the word by the position of the cursor. In other words, I would like to change the behavior of the macro in the following way: If no text is selected, then the search string should be the word in which (or at the end of which) the cursor is currently located. But if a certain portion of text is selected, the search string should be the selected text.

Can that be done? If so, can someone tell me how the above macro needs to be modified in order to achieve it?

Thanks in advance for your help!