Wednesday, June 24, 2026

Add Web Search to LibreOffice Writer

Introduction

In Vivaldi1 I can right click on a word or selected phrase and search for it. But in LibreOffice Writer, I'd have to copy the selection to my clipboard, switch to the browser, paste it into the search bar and then press Enter. I longed to have a generic search capability for Writer. So I created it.

Well, first I searched for it, and I found a macro that was designed to do something similar2. It provided the Basic code I needed to read the selected text.3 It had to be modified somewhat to work on my computer, but it was a tremendous help.

Then I built on it. The collection of macros that I wrote allows a user to perform the search using several search engines. And it's amenable to configuration in the Context Menu.

Workflow Summary -- Search the Internet for selected text
  • Select some text.
  • Right click on it.
  • Search the Internet for it.
  • Use any one of these these preconfigured search engines:
    • DuckDuckGo
    • Google
    • LinkedIn
    • Wikipedia
    • Open Library
    • The Free Dictionary
    • And you can add your own!

Detail

Web Search is a LibreOffice Basic module that contains the macros needed to perform an Internet search of selected text.

Program Flow

A unique macro is dedicated to each search engine. The user selects text and then invokes the macro that corresponds to the desired search engine.

The invoked macro sets an index that points to the search engine URL in an array. Then it invokes the Main subroutine.

Main calls an Initialize routine, which loads the path to the web browser executable and all the Search Engine URLs.

Main performs two checks. First it verifies that the search engine index is within the bounds of the array of search engine URLs. The out-of-the-box state is that the index should be in the range 0 to 5.

The second check is that Main verifies that text has been selected in the Writer document.

If and only if the previous two checks are successful, then Main calls the InvokeSearch routine; the search engine URL and the search terms are its arguments. If either check fails, Main and the invoked macro terminate; no action is performed.

InvokeSearch replaces a token in the search engine URL with the search terms. Then it calls the system shell, which opens the web browser with the conditioned search engine URL as an argument. The shell process is asynchronous.

The web browser takes focus and displays search results.

Finally, the three procedures (two subroutines and a function) are removed from the call stack, and the global variables are cleared.

Context Menu Setup

The LibreOffice Context Menu can be configured to provide easy access to each Search Engine macro. The user can right click on selected text, choose "Search on Web...," and then choose the submenu item that corresponds to the desired search engine. In Figure 1 below, the DuckDuckGo search engine is selected. Thus each Web Search submenu item is assigned a unique macro.

Figure 1: The SearchWeb Context Menu


Configure the context menu as shown in Figure 2, below. Access this dialog window by selecting Tools | Customize on the menu and then click the Context Menu tab.

Figure 2: Customizing the Context Menu


The corresponding XML content is contained in the text.xml file, which is located here in the user's profile:

AppData\Roaming\LibreOffice\4\user\config\soffice.cfg\modules\swriter\popupmenu\

The first 20 lines of text.xml are provided in the listing below. Note that the menu items for Open Library and The Free Dictionary are wrapped.

It may be easier to edit this file than to use the Customize form that's shown above.

Contents of text.xml

<?xml version="1.0" encoding="UTF-8"?>
-
<menu:menupopup xmlns:menu="http://openoffice.org/2001/menu">
  -
  <menu:menu menu:label="Search on Web..." menu:id="Search on Web...">
    -
    <menu:menupopup>
	<menu:menuitem menu:label="D DuckDuckGo" menu:id="vnd.sun.star.script:Standard.SearchWeb.SEDuckDuckGo?language=Basic&location=application"/>
	<menu:menuitem menu:label="G Google" menu:id="vnd.sun.star.script:Standard.SearchWeb.SEGoogle?language=Basic&location=application"/>
	<menu:menuitem menu:label="L LinkedIn" menu:id="vnd.sun.star.script:Standard.SearchWeb.SELinkedIn?language=Basic&location=application"/>
	<menu:menuitem menu:label="O Open Library" menu:id="vnd.sun.star.script:Standard.SearchWeb.SEOpenLibrary?language=Basic&location=application"/>
	<menu:menuitem menu:label="F The Free Dictionary" menu:id="vnd.sun.star.script:Standard.SearchWeb.SETheFreeDictionary?language=Basic&location=application"/>
	<menu:menuitem menu:label="W Wikipedia" menu:id="vnd.sun.star.script:Standard.SearchWeb.SEWikipedia?language=Basic&location=application"/>
    </menu:menupopup>
  </menu:menu>
  <menu:menuitem menu:id=".uno:NoBreak"/>
  <menu:menuseparator/>
  <menu:menuitem menu:id=".uno:Cut"/>
  <menu:menuitem menu:id=".uno:Copy"/>
  <menu:menuitem menu:id=".uno:Paste"/>


1 Vivaldi, https://vivaldi.com/desktop/
2 Ratslinger's post in this thread on the LibreOffice help forum provided a solution way back in September 2018 https://ask.libreoffice.org/t/link-to-browser-in-writer/35943/6
3 Here's a function that returns the selected text; words in a phrase are separated+with+plus+signs+instead+of+spaces:
    Private Function GetSelection() As String
      ' Returns Selected Text
      ' 2025-12-22 Created
      Dim oSelections, oFirstSelection As Object
      oSelections = ThisComponent.CurrentSelection
      oFirstSelection = oSelections.getByIndex(0)
      strTemp = oFirstSelection.String
      If strTemp <> "" Then
        GetSelection = Replace(strTemp, " ", "+")
      End If
    End Function

No comments:

Post a Comment

Thank you for taking time to comment. Please note that comments may be deleted if they:

* Contain link(s) to a domain outside USA or Canada or to a commercial enterprise.
* Include non-English words or characters.
* Are irrelevant to the subject matter of the post.

For posts older than 14 days, comments are moderated and require approval.